示例#1
0
def help_command(_query, slack_event):
    """
    Sends a private message to user containing information on commands known

    query - query str(unused for this function)
    slack_event - A dict of slack event information

    Returns empty string
    """

    # get users direct message channel id
    dm_request = json.loads(request.submit_slack_request({'user': slack_event['user']}, 'im.open'))
    dm_id = dm_request['channel']['id']

    text = "Known Commands:\n"
    for command, info, in commands().items():
        line_text = '`' + command + '` - ' + info['description'] + "\n"
        text = text + line_text

    # Post list of commands to user in direct message
    request.submit_slack_request({'channel': dm_id, 'text': text}, 'chat.postMessage')

    # react to current message with an emoji
    data = {
        'name': 'diddy_boom_box',
        'channel': slack_event["channel"],
        'timestamp': slack_event['ts'],
    }
    request.submit_slack_request(data, 'reactions.add')

    return ''
示例#2
0
def kill_me(_query, slack_event):
    """
    query - query str (unused for this function)
    slack_event - A dict of slack event information

    Returns nothing
    """

    # kick user from channel
    data = {
        'channel': slack_event["channel"],
        'user': slack_event['user'],
    }
    response = json.loads(request.submit_slack_request(data, 'channels.kick', 'USER'))

    # DM User with fun line
    if response['ok']:
        text = random.choice([
            'By order of the SDF Armed Forces you have been killed',
            'Ask not for whom the bell tolls. It tolls for thee ~ John Donne',
            "We all have but one life to live. Well you do. I'm a bot, and I have backups",
            ('A coward dies a thousand times before their death, but the valiant '
             'taste of death but once.\n ~ William Shakespeare'),
            ('Do not go gentle into that good night\n'
             'Old age should burn and rave at close of day; \n'
             'Rage, rage against the dying of the light. \n ~ Dylan Thomas'),
            ('Life is cruel. Of this I have no doubt. '
             'One can only hope that one leaves behind a lasting legacy. '
             'But so often, the legacies we leave behind...are not the ones we intended.\n'
             '~ Queen Myrrah ~ Gears of War 2'),
            ('Death is inevitable. Our fear of it makes us play safe, blocks out emotion. '
             "It's a losing game. Without passion, you are already dead. \n~Max Payne"),
            ('The ending isn’t any more important than any of the moments leading to it.\n'
             '~ Dr Rosalene (To The Moon)'),
            "Omae wa Mou Shindeiru!",
            ('Stop pitying yourself. Pity yourself, and life becomes an endless nightmare.\n'
             '~ Osamu Dazai (Bungo Stray Dogs)'),
            "Heghlu’meH QaQ jajvam ~ Klingon Proverb",
            "batlhbIHeghjaj ~ Klingon Proverb"
        ])
    elif response['error'] == 'cant_kick_from_general':
        text = "The top brass told me I can't kick people from workspaces anymore. :pouting_cat:"
    else:
        text = f"I can't get that done. I get this error: `{response['error']}`"

    dm_id = request.direct_message_channel_search(slack_event)
    request.submit_slack_request({'channel': dm_id, 'text': text}, 'chat.postMessage')

    # React to users message asking the bot to kill them
    data = {
        'name': 'dead4',
        'channel': slack_event["channel"],
        'timestamp': slack_event['ts'],
    }
    request.submit_slack_request(data, 'reactions.add')

    return ''
示例#3
0
def lambda_handler(data, _context):
    """
    Handle an incoming HTTP request from a Slack chat-bot.
    data: slack request object
    _context: context object from awk
    """

    if not valid_slack_request(data):
        return request.return_status()

    raw_body = json.loads(data['body'])

    if 'challenge' in raw_body:
        return {
            'statusCode': 200,
            'body': raw_body['challenge'],
        }

    slack_event = raw_body['event']

    if "bot_id" in slack_event:  # Prevent bot from responding to itself
        return request.return_status()

    body = {}

    chat_action = 'chat.postMessage'
    if slack_event['type'] in ["message", "app_mention"]:
        # Get the ID of the channel where the message was posted.
        body = {
            'channel': slack_event["channel"],
            'text': message_text(slack_event),
            'unfurl_media': 'true',
            'unfurl_links': 'true'
        }
        if 'thread_ts' in slack_event:
            body['thread_ts'] = slack_event['thread_ts']

    # Currently the emoji that causes the delete logic is 'delet' (Yes it's mispelled)
    elif 'reaction_added' in slack_event['type'] and 'delet' in slack_event[
            "reaction"]:
        chat_action = 'chat.delete'
        body = {
            'channel': slack_event['item']['channel'],
            'ts': slack_event['item']['ts']
        }

    body = response_helper.apply_additional_message_options(body)
    request.submit_slack_request(body, chat_action)

    return request.return_status()
示例#4
0
def lambda_handler(data, _context):
    """
    Handle an incoming HTTP request from a Slack chat-bot.
    data: slack request object
    _context: context object from awk
    """
    if not valid_slack_request(data):
        return request.return_status()

    slack_event = json.loads(data['body'])['event']

    if "challenge" in slack_event:
        return slack_event["challenge"]

    if "bot_id" in slack_event: # Prevent bot from responding to itself
        return request.return_status()

    chat_action = 'chat.postMessage'
    if 'app_mention' in slack_event['type']:
        # Get the ID of the channel where the message was posted.
        data = {
            'channel': slack_event["channel"],
            'text': message_text(slack_event),
            'unfurl_media': 'true',
            'unfurl_links': 'true'
        }
        if 'thread_ts' in slack_event:
            data['thread_ts'] = slack_event['thread_ts']
    # Currently the emoji that causes the delete logic is 'delet' (Yes it's mispelled)
    elif 'reaction_added' in slack_event['type'] and 'delet' in slack_event["reaction"]:
        chat_action = 'chat.delete'
        data = {
            'channel': slack_event['item']['channel'],
            'ts': slack_event['item']['ts']
        }

    request.submit_slack_request(data, chat_action)
    return request.return_status()
示例#5
0
def summon(query, slack_event):
    data = {
        'users': [slack_event["user"]],
        'channel': slack_event["channel"]
     }

    response = json.loads(request.submit_slack_request(data, 'conversations.invite'))

    if response["ok"] == 'true':
        return 'Get in here {}!'.format(slack_event["user"])
    elif response["ok"] == 'false':
        if response["error"] == 'user_not_found':
            return "I'm not sure who {} is".format(slack_event["user"])
        elif response['error'] == 'already_in_channel':
            return "{} is already in here!".format(slack_event["user"])
        else:
            return 'some other error'
    else:
        return response
示例#6
0
def run_command(command, query, message_user):
    """
    runs the passed in command and query
    :command what to run (image me, gif me etc)
    :query what to search for
    :message_user the user who sent the request
    :Returns the result of the bot command
    """

    text = ''
    if not command: text = 'Did you need something?'
    if command in ["anime", 'manga']: text = request.anime_news_network_search(command, query)
    elif command in ["image", "img"]: text = fetch_image(query)
    elif command == 'reverse': text = query[::-1]
    elif command == 'youtube': text = request.youtube_search(query)
    elif command in ['wiki', 'wikipedia']: text = request.wikipedia_search(query)
    elif command in ['gif', 'sticker']: text = request.gify_search(command, query)
    elif command == 'tableflip': text = random.choice(["(ノಥ益ಥ)ノ ┻━┻ ", "┻━┻ ︵ヽ(`Д´)ノ︵ ┻━┻    ", "(ノಠ益ಠ)ノ彡┻━┻ ", "ヽ(`Д´)ノ┻━┻", " (ノ≥∇))ノ┻━┻ ", "(╯°□°)╯︵ ┻━┻ "])
    elif command == 'putitback': text = random.choice(["┬─┬ノ( º _ ºノ)", r"┬──┬ ¯\_(ツ)"])
    elif command == 'flipcoin': text = f":coin: :coin: {random.choice(['HEADS', 'TAILS'])} :coin: :coin:"
    elif command == 'decide': text = f"I'm gonna go with: {random.choice(query.split(' '))}"
    elif command == 'callthecops': text = 'You called? ' + fetch_image('anime cops from bing')
    elif command == 'call' and query == 'the cops': text = 'You called? ' + fetch_image('anime cops from bing')
    elif command == 'kill': text = request.gify_search('gif', 'kill me')
    elif command == 'shame':
        user = query.strip().upper()
        text = random.choice([
            'Shame on you! ' + user + 'You should know better!',
            user + ' ಠ_ಠ',
            user + ' You have made a mockery of yourself. Turn in your weeabo credentials!',
            user + ' :blobdisapproval:',
            user + ' :disappoint:',
            user + ' you did bad and you should feel bad',
            user + ' :smh:',
        ])
    elif command == 'help':
        # get users direct message channel id
        dm_id = json.loads(request.submit_slack_request({'user': message_user}, 'im.open'))['channel']['id']
        data = {
            'channel': dm_id,
            'text':
            """
            Known Commands:

            reverse me - reverse text entered after command
            image me (alias img me) - use text after command to search for an image (use suffix "from [google|bing]" if you want to specify a search engine)
            youtube me - use text after command to query youtube for a video
            anime me - use text after command to query anime news network for anime info
            manga me - use text after command to query anime news network for manga info
            wiki me - use text after command to query wikipedia for an article
            gif me - use text after command to query giphey for gif
            sticker me - use text after command to query giphey for sticker
            tableflip - responds with a tableflip emoji
            putitback - responds with a reversetableflip emoji
            flipcoin - responds with either head or tails
            decide - responds randomly with one of the words after this command
            callthecops (alias call the cops) - responds with an image of anime cops with the caption "You Called"
            kill me - responds with a gif with kill me as a search
            shame - shame user (You must @USER_NAME)
            """
        }

        # Post list of commands to user in direct message
        request.submit_slack_request(data, 'chat.postMessage')
        # Tell user to check their direct messages
        text = f"<@{message_user}>" + ' check your Direct Messages for the list of my known commands.'
    else: text = "Sorry I don't know that command. To see all my commands use `help`"

    if not text: text = "Sorry no results found"

    return text