-
Notifications
You must be signed in to change notification settings - Fork 14
/
bot.py
118 lines (100 loc) · 3.76 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
"""
This module runs basically everything.
Attributes:
VERSION = "2.0.0" (String): Version Number: release.version_num.revision_num
# Config Variables
EMOJI_LIST (List): List of Strings for emojis to be added to announcements
USER_LIST (JSON): List of users in JSON format
TTPB (String): Config variable, sets channel for cleverbot integration
GENERAL (Stirng): Config variable, sets channel for general's name
LOG (boolean): Global Variable
LOGC (boolean): Global Variable
pbCooldown (int): Global Variable
.. _Google Python Style Guide:
http://google.github.io/styleguide/pyguide.html
"""
from slackclient import SlackClient
import threading, websocket, json, re, time, codecs, random, os
import scripts
from scripts import commands
from pb_logging import PBLogger
import logging
logger = PBLogger('Bot')
class Bot(object):
EMOJI_LIST = ["party-parrot", "venezuela-parrot", "star2", "fiesta-parrot", "wasfi_dust", "dab"]
GENERAL_CHANNEL = ""
TTPB = "talk-to-pantherbot"
VERSION = "2.1.0"
def __init__(self, token, bot_name=""):
self.SLACK_CLIENT = None
self.BOT_NAME = bot_name
self.BOT_ICON_URL = "http://i.imgur.com/QKaLCX7.png"
self.USER_LIST = None
self.POLLING_LIST = dict()
self.WEBSOCKET = None
self.THREADS = []
self.COMMANDS_LIST = commands
self.pb_cooldown = True
# self.check_in_thread_ts
self.create_slack_client(token)
def create_slack_client(self, token):
self.SLACK_CLIENT = SlackClient(token)
# Returns a list of channel IDs searched for by name
def channels_to_ids(self, channel_names):
pub_channels = self.SLACK_CLIENT.api_call(
"channels.list",
exclude_archived=1
)
pri_channels = self.SLACK_CLIENT.api_call(
"groups.list",
exclude_archived=1
)
list_of_channels = []
for channel in pub_channels["channels"]:
for num in range(0, len(channel_names)):
if channel["name"].lower() == channel_names[num].lower():
list_of_channels.append(channel["id"])
# Same as above
for channel in pri_channels["groups"]:
for num in range(0, len(channel_names)):
if channel["name"].lower() == channel_names[num].lower():
list_of_channels.append(channel["id"])
return list_of_channels
# Send Message
# Sends a message to the specified channel (looks up by channel name, unless is_id is True)
def send_msg(self, channel, text, is_id=False, thread_ts=None):
if is_id:
channel_id = channel
else:
channel_id = self.channels_to_ids([channel])[0]
if thread_ts is not None:
response_json = self.SLACK_CLIENT.api_call(
"chat.postMessage",
channel=channel_id,
text=text,
username=self.BOT_NAME,
icon_url=self.BOT_ICON_URL,
thread_ts=thread_ts
)
else:
response_json = self.SLACK_CLIENT.api_call(
"chat.postMessage",
channel=channel_id,
text=text,
username=self.BOT_NAME,
icon_url=self.BOT_ICON_URL
)
logger.info("Message sent: ")
return response_json
def emoji_reaction(self, channel, ts, emoji):
self.SLACK_CLIENT.api_call(
"reactions.add",
name=emoji,
channel=channel,
timestamp=ts
)
logger.info("Reaction posted: " + emoji)
def close(self):
self.WEBSOCKET.keep_running = False
logger.info("Closing Websocket...")