def get_direct_im_channel(workspace, user_id): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None json_post = {'user': user_id, 'return_im': True} headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': f"Bearer {token}" } if current_app.config.get('FAKE_SLACK'): return json.loads('''{ "ok": true, "channel": { "id": "D947RLWRX" } }''') result = urlfetch.post(url="https://slack.com/api/im.open", data=json.dumps(json_post), headers=headers) current_app.logger.debug(result.content) return json.loads(result.content)
def leave_channel(workspace, channel_id): if current_app.config.get('FAKE_SLACK'): return json.loads('''{ "ok": true }''') token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None myself = auth_test(token) user_id = myself['user_id'] token = settings.get_access_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None json_post = {'channel': channel_id, 'user': user_id} headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': f"Bearer {token}" } result = urlfetch.post(url="https://slack.com/api/channels.kick", data=json.dumps(json_post), headers=headers) current_app.logger.debug(result.content) return json.loads(result.content)
def get_userinfo(workspace, user_id): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None if current_app.config.get('FAKE_SLACK'): return json.loads('''{ "ok": true, "user": { "id": "W012A3CDE", "team_id": "T012AB3C4", "name": "spengler", "deleted": false, "color": "9f69e7", "real_name": "Egon Spengler", "tz": "America/Los_Angeles", "tz_label": "Pacific Daylight Time", "tz_offset": -25200, "profile": { "avatar_hash": "ge3b51ca72de", "status_text": "Print is dead", "status_emoji": ":books:", "real_name": "Egon Spengler", "display_name": "spengler", "real_name_normalized": "Egon Spengler", "display_name_normalized": "spengler", "email": "*****@*****.**", "image_24": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_32": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_48": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_72": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_192": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_512": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "team": "T012AB3C4" }, "is_admin": true, "is_owner": false, "is_primary_owner": false, "is_restricted": false, "is_ultra_restricted": false, "is_bot": false, "updated": 1502138686, "is_app_user": false, "has_2fa": false } }''') headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8' } result = urlfetch.get( url="https://slack.com/api/users.info?user=%s&token=%s" % (user_id, token), headers=headers) return result
def get_channel_members(workspace, channel, cursor): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': f"Bearer {token}" } result = urlfetch.post( url= "https://slack.com/api/conversations.members?channel=%s&cursor=%s&limit=1000" % (channel, cursor), headers=headers) return result.json
def command_reply(workspace, url, message): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': 'Bearer %s' % token } if current_app.config.get('FAKE_SLACK'): current_app.logger.info(url) current_app.logger.info(str(message)) return '{"ok": true}' result = urlfetch.post(url=url, data=json.dumps(message), headers=headers) return result
def post_attachment(workspace, post): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': 'Bearer %s' % token } if current_app.config.get('FAKE_SLACK'): current_app.logger.info(str(post)) return '{"ok": true}' result = urlfetch.post(url="https://slack.com/api/chat.postMessage", data=json.dumps(post), headers=headers) return result
def dialog_open(workspace, trigger_id, dialog): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': f"Bearer {token}" } json_post = {'trigger_id': trigger_id, 'dialog': dialog} if current_app.config.get('FAKE_SLACK'): current_app.logger.info(str(json_post)) return json.loads('''{"ok": true}''') result = urlfetch.post(url="https://slack.com/api/dialog.open", data=json.dumps(json_post), headers=headers) current_app.logger.debug(result.content) return json.loads(result.content)
def get_users(workspace, cursor): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': f"Bearer {token}" } if current_app.config.get('FAKE_SLACK'): return json.loads('''{ "ok": true, "members": [ { "id": "W012A3CDE", "team_id": "T012AB3C4", "name": "spengler", "deleted": false, "color": "9f69e7", "real_name": "spengler", "tz": "America/Los_Angeles", "tz_label": "Pacific Daylight Time", "tz_offset": -25200, "profile": { "avatar_hash": "ge3b51ca72de", "status_text": "Print is dead", "status_emoji": ":books:", "real_name": "Egon Spengler", "display_name": "spengler", "real_name_normalized": "Egon Spengler", "display_name_normalized": "spengler", "email": "*****@*****.**", "image_24": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_32": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_48": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_72": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_192": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "image_512": "https://.../avatar/e3b51ca72dee4ef87916ae2b9240df50.jpg", "team": "T012AB3C4" }, "is_admin": true, "is_owner": false, "is_primary_owner": false, "is_restricted": false, "is_ultra_restricted": false, "is_bot": false, "updated": 1502138686, "is_app_user": false, "has_2fa": false }, { "id": "W07QCRPA4", "team_id": "T0G9PQBBK", "name": "glinda", "deleted": false, "color": "9f69e7", "real_name": "Glinda Southgood", "tz": "America/Los_Angeles", "tz_label": "Pacific Daylight Time", "tz_offset": -25200, "profile": { "avatar_hash": "8fbdd10b41c6", "image_24": "https://a.slack-edge.com...png", "image_32": "https://a.slack-edge.com...png", "image_48": "https://a.slack-edge.com...png", "image_72": "https://a.slack-edge.com...png", "image_192": "https://a.slack-edge.com...png", "image_512": "https://a.slack-edge.com...png", "image_1024": "https://a.slack-edge.com...png", "image_original": "https://a.slack-edge.com...png", "first_name": "Glinda", "last_name": "Southgood", "title": "Glinda the Good", "phone": "", "skype": "", "real_name": "Glinda Southgood", "real_name_normalized": "Glinda Southgood", "display_name": "Glinda the Fairly Good", "display_name_normalized": "Glinda the Fairly Good", "email": "*****@*****.**" }, "is_admin": true, "is_owner": false, "is_primary_owner": false, "is_restricted": false, "is_ultra_restricted": false, "is_bot": false, "updated": 1480527098, "has_2fa": false } ], "cache_ts": 1498777272, "response_metadata": { "next_cursor": "dXNlcjpVMEc5V0ZYTlo=" } }''') result = urlfetch.post( url="https://slack.com/api/users.list?cursor=%s&limit=1000" % cursor, headers=headers) # self.log.debug(result.content) return json.loads(result.content)
def get_channelinfo(workspace, channel_id): token = settings.get_bot_token(workspace) if not token: current_app.logger.warning( f"Requested token for workspace {workspace} but found none") return None headers = { 'User-Agent': f'karmabot/{karmabot.__version__}', 'Content-Type': 'application/json; charset=utf-8', 'Authorization': f"Bearer {token}" } if current_app.config.get('FAKE_SLACK'): return json.loads(''' { "ok": true, "channel": { "id": "C1H9RESGL", "name": "busting", "is_channel": true, "created": 1466025154, "creator": "U0G9QF9C6", "is_archived": false, "is_general": false, "name_normalized": "busting", "is_shared": false, "is_org_shared": false, "is_member": true, "is_private": false, "is_mpim": false, "last_read": "1503435939.000101", "latest": { "text": "Containment unit is 98% full", "username": "******", "bot_id": "B19LU7CSY", "attachments": [ { "text": "Don't get too attached", "id": 1, "fallback": "This is an attachment fallback" } ], "type": "message", "subtype": "bot_message", "ts": "1503435956.000247" }, "unread_count": 1, "unread_count_display": 1, "members": [ "U0G9QF9C6", "U1QNSQB9U" ], "topic": { "value": "Spiritual containment strategies", "creator": "U0G9QF9C6", "last_set": 1503435128 }, "purpose": { "value": "Discuss busting ghosts", "creator": "U0G9QF9C6", "last_set": 1503435128 }, "previous_names": [ "dusting" ] } }''') result = urlfetch.get( url="https://slack.com/api/channels.info?channel=%s" % channel_id, headers=headers) return json.loads(result.content)