Esempio n. 1
0
def process_message(data):
    '''If a user passes 'print users' in a message, print the users in the slack
    team to the console. (Don't run this in production probably)'''

    if 'print users' in data['text']:
        for user in sc.api_call("users.list")["members"]:
            print(user["name"], user["id"])
Esempio n. 2
0
def process_message(data):
    text = data.get("text")

    users = {x["id"]: x["name"] for x in sc.api_call("users.list")["members"]}

    r = re.match('^(.*)?!roll\s*(.*?)$', text, re.IGNORECASE)
    result = "%s: %s" % (users[data["user"]], do_rolls(r.group(2))[1])
    outputs.append([data["channel"], result])
Esempio n. 3
0
def get_username_from_id(userid):
    users = sc.api_call('users.list')

    for user in users['members']:
        if user['id'] == userid:
            return user['name']

    return False
Esempio n. 4
0
def _send_message(user_id, message, channel_id=None):
    if channel_id is not None:
        message = "<@%s> %s" % (user_id, message)
    else:
        result = sc.api_call('im.open', user=user_id)
        if "ok" in result and result["ok"]:
            channel_id = result["channel"]["id"]
    if channel_id is not None:
        outputs.append([channel_id, message])
Esempio n. 5
0
def log(data):
    global LAST_CHANNEL

    logdir = os.environ["SLACK_LOGDIR"]
    botname = os.environ["SLACK_BOTNAME"]

    users = {x["id"]: x["name"] for x in sc.api_call("users.list")["members"]}
    channels = {
        x["id"]: x["name"]
        for x in sc.api_call("channels.list")["channels"]
    }
    # Direct Messages
    ims = {x["id"]: users[x["user"]] for x in sc.api_call("im.list")["ims"]}
    # Multi User Direct Messages and Private Groups
    groups = {x["id"]: x["name"] for x in sc.api_call("groups.list")["groups"]}

    try:
        user = users[data['user']]
    except KeyError:
        user = "******" % (botname, )

    try:
        if data['channel'][0] == "C":
            channel = "#%s" % (channels[data['channel']], )
        elif data['channel'][0] == "D":
            channel = ims[data['channel']]
        elif data['channel'][0] == "G":
            channel = groups[data['channel']]
        elif data['channel'][0] == "U":
            channel = users[data['channel']]
        LAST_CHANNEL = channel
    except KeyError:
        if data.get('channel'):
            channel = data['channel']
        else:
            channel = LAST_CHANNEL

    with open("%s/%s" % (logdir, channel), 'a') as logfile:
        logfile.write("[%s] %s: %s\n" % (time.ctime(int(float(
            (data['ts'])))), user, data['text']))
    return
Esempio n. 6
0
def bot_message_delete(data):
    userid = data['user']

    try:
        username = get_username_from_id(userid)
    except:
        outputs.append([
            data['item']['channel'],
            "Failed to get username for user ID: {0}".format(userid)
        ])
        return False

    user_is_authorized = is_user_authorized(
        userid, username, bot_message_delete_auth_usernames,
        bot_message_delete_auth_group_handles)

    if data['reaction'] in bot_message_delete_reactions and user_is_authorized:
        response = sc.api_call('chat.delete',
                               ts=data['item']['ts'],
                               channel=data['item']['channel'])
Esempio n. 7
0
import re

# Import variables from config file
config = yaml.load(open('plugins/servicenow.conf', 'r'))

instance = config.get('SERVICENOW_INSTANCE')
search_regex = config.get('SERVICENOW_SEARCH_REGEX')

outputs = []

# The RTM API sometimes processes the bot's last posted message
# upon connection. This can result in the bot posting duplicate URL's
# when it connects under certain circumstances (e.g. disconnect/reconnect).
# Also, there's probably few, if any, circumstances where the bot
# responding to messages it posted would be desired behavior.
bot_user_id = sc.api_call('auth.test')['user_id']


def process_message(data):
    # If the message was not posted by the Slack bot
    if data['user'] != bot_user_id:
        # Split message content up by whitespace
        strings = re.split('\s+', data['text'])

        # For each grouping of characters
        for string in strings:
            # Remove non-alphanumeric characters
            string = re.sub('\W+', '', string)

            # Search for a regex pattern match
            ticket = re.match(search_regex, string, re.IGNORECASE)
from __future__ import unicode_literals
from client import slack_client as sc

for user in sc.api_call("users.list")["members"]:
    print(user["name"], user["id"])