Exemple #1
0
def execute(command, user):
    # import on execution
    from noob_snhubot import slack_client

    # Get IM list
    channels_info = slack_client.api_call("channels.list")
    channels = {}  # dictionary for listing all channels in workspace

    # Get the channels list and prep for mention expantion
    if channels_info.get("ok"):
        for channel in channels_info.get("channels"):
            id = channel.get("id")
            purpose = channel.get("purpose").get("value")

            channels[id] = purpose  # Add new dictionary item with purpose

    # Process channels dictionary for output
    #Trying to make a pretty table attachment
    #fields = []
    #for k, v in channels.items():
    #    fields.append({"title": "<#{}>".format(k), "value": "{}".format(v)})
    #
    #output = {
    #    "link_names": 1,
    #    "parse": "full",
    #    "pretext": "_*Here's a detailed list of our channels for your convenience.*_",
    #    "text": "Click on the links to view the channels and start chatting!",
    #    "fields": fields
    #}
    #
    #attachment = json.dumps([output])
    attachment = None
    response = None

    channel_output = ""
    for k, v in channels.items():
        channel_output += "<#{}>: {}\n".format(k, v)

    response = """
_*Here's a detailed list of our channels for your convenience.*_
Click on the links to view the channels and start chatting!
{}
""".format(channel_output)

    return response, attachment
Exemple #2
0
def execute(command, user):
    from noob_snhubot import slack_client

    bot_id = slack_client.api_call("auth.test")["user_id"]
    response = None
    attachment = None
    cmd = command.split()

    if len(cmd) > 1:
        topic = cmd[1].lower()
        try:
            attachment = build_attachment(topic)
        except KeyError:
            response = "I'm sorry, Dave.  I'm afraid I can't do that. Try `<@{}> it140` for a list of valid topics.".format(
                bot_id)
    else:
        response = "Here is a list of valid IT-140 topics.  Proceed by entering: `<@{}> it140 topic`. \n\n".format(bot_id) \
            + "\n".join("- `{}`".format(x) for x in data.keys() if x != "it140")

    return response, attachment
Exemple #3
0
def execute(command, user):
    # import on execution
    from noob_snhubot import slack_client

    # Get IM list
    team_info = slack_client.api_call("team.info")
    channels_info = slack_client.api_call("channels.list")
    channels = {}  # dictionary for listing all channels in workspace
    bot_id = slack_client.api_call("auth.test")["user_id"]

    # Set the Team Name
    if team_info.get("ok"):
        team_name = team_info.get("team").get("name")
    else:
        team_name = "snhu_coders"

    # Get the channels list and prep for mention expantion
    if channels_info.get("ok"):
        for channel in channels_info.get("channels"):
            id = channel.get("id")
            name = channel.get("name")
            purpose = channel.get("purpose").get("value")

            if name == "general":
                general = id

            channels[id] = purpose  # Add new dictionary item with purpose

    # Process channels dictionary for output
    channel_output = ""
    for k, v in channels.items():
        channel_output += "<#{}>: {}\n".format(k, v)

    # open the IM channel to the new user
    im_channel = slack_client.api_call("im.open", user=user)

    #print("IM CHANNEL:")
    #print(im_channel)

    greeting = """
_Welcome to *{0}*, <@{1}>!_ 

We're so happy that you've joined our community! Please introduce yourself in <#{3}>, and let us know what brings you to the team! 
*{0}* is a place for people to _*learn*_, _*collaborate*_, _*network*_, and just hang out. Please be kind to each other, and _encourage_ learning!

I am <@{2}>, your friendly protocol droid. You may issue commands to me in any channel I'm present in _(even this one)_!
Use `<@{2}> help` to _*learn more about the commands*_ I respond to. For a _*detailed list of our channels*_, please issue the `<@{2}> channels` command.

*RULES TO LIVE BY:*
1. *Do not give away, nor expect, direct answers to homework assignments*. This is a learning community and cheating will not be tolerated.
2. *Do not post requests for help in multiple channels*. Find an appropriate channel for your request and be patient. Someone will be along to help you in time.
3. *Use code snippets*! See below for help posting code snippets. *Do not cut and paste code samples directly into chat*. It's impossible to read.

*More about Slack:*
_If you're new to Slack_, please check out the <https://get.slack.help/hc/en-us/articles/217626358-Tour-the-Slack-app#windows-app-1|Slack Tour>.
_A handy feature of Slack_ is the ability to <https://get.slack.help/hc/en-us/articles/204145658-Create-a-snippet|Create a Snippet>.

""".format(team_name, user, bot_id, general)

    # Return the new greeting and send private message
    return greeting, im_channel.get("channel").get("id") if im_channel.get(
        "ok") else (None, None)
import re
import json
from noob_snhubot import DB_CONFIG
from BotHelper import Catalog, Course
from BotHelper.HashTable import HashTable

command = "catalog"
public = True
disabled = False

# Check if we're running with a database connection
if DB_CONFIG:
    # perform imports
    from noob_snhubot import mongo, slack_client

    bot_id = slack_client.api_call("auth.test")["user_id"]
    
    # change to catalog/subjects context
    mongo.use_db('catalog')
    mongo.use_collection('subjects')
    
    # get all documents
    data = mongo.find_documents({})

    # create Catalog using HashTable implementation
    catalog = Catalog()
    for subject in data:
        course_data = HashTable(47)
        for course in subject['courses']:
            course_data[course['id']] = Course(course['title'], course['description'], course['credits'], course['requisites'])
        catalog.subjects[subject['title']] = course_data
Exemple #5
0
def execute(command, user):
    from noob_snhubot import slack_client
    bot_id = slack_client.api_call("auth.test")["user_id"]

    ROLL_REGEX = "^([1-9][0-9]{0,2})d([1-9][0-9]{0,2})(([+.-])(\d+))?$"
    roll = command.split()

    attachment = None
    response = "That roll is not valid. Try `<@{}> roll help`".format(bot_id)

    if len(roll) > 1:
        roll = command.split()[1]

        if roll.lower().startswith("help"):
            # PEP8 style for long strings
            response = (
                "Let me show you how I roll:\n`roll XdY[±Z]` "
                "rolls X number of Y-Sided dice with an optional positive or "
                "negative Z modifier.\n"
                "Examples:\n`"
                "<@{0}> roll 2d6`\n`<@{0}> roll 1d10+3`\n`<@{0}> roll 20d4-5`"
            ).format(bot_id)
        else:
            match = re.match(ROLL_REGEX, roll)

            if match is not None:
                rolls = []
                num_dice = int(match.groups()[0])
                pips = int(match.groups()[1])

                for x in range(num_dice):
                    rolls.append(random.randint(1, pips))

                total = sum(rolls)

                if match.groups()[2] is not None:
                    mod = int(match.groups()[4])

                    if match.groups()[3] == "+":
                        total += mod
                    else:
                        total -= mod

                attachment = json.dumps([{
                    "text":
                    "<@{}> rolled *{}*".format(user, total),
                    "fields": [{
                        "title": "Roll",
                        "value": "{}".format(match.group()),
                        "short": "true"
                    }, {
                        "title":
                        "Values",
                        "value":
                        "{}".format(" ".join(str(roll) for roll in rolls)),
                        "short":
                        "true"
                    }, {
                        "title": "Modifier",
                        "value": "{}".format(match.groups()[2]),
                        "short": "true"
                    }],
                    "color":
                    "good"
                }])

    return response, attachment