-
Notifications
You must be signed in to change notification settings - Fork 20
/
bot.py
executable file
·134 lines (103 loc) · 3.87 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
#
# Slack.com Python bot using Amazon SNS/Incoming WebHook integration
#
import sys
import os
import re
import time
import json
import requests
import importlib
import traceback
import boto.sqs
from boto.sqs.message import RawMessage
from glob import glob
from daemon import runner
curdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(curdir)
from config import config
queue_name = config.get("queue")
aws_access_key = config.get("aws_access_key")
aws_secret_key = config.get("aws_secret_key")
region = 'us-east-1'
hooks = {}
conn = boto.sqs.connect_to_region(region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
q = conn.get_queue(queue_name)
q.set_message_class(RawMessage)
DEPLOY_DAEMON_PID = "/tmp/slackbot.pid"
DEPLOY_DAEMON_ERROR = "/tmp/slackbot.log"
class StartBot():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/null'
self.stderr_path = DEPLOY_DAEMON_ERROR
self.pidfile_path = DEPLOY_DAEMON_PID
self.pidfile_timeout = 5
def run(self):
while True:
get_msg()
time.sleep(1)
def init_plugins():
for plugin in glob('plugins/[!_]*.py'):
print "plugin: %s" % plugin
try:
mod = importlib.import_module(plugin.replace("/", ".")[:-3])
modname = mod.__name__.split('.')[1]
for hook in re.findall("on_(\w+)", " ".join(dir(mod))):
hookfun = getattr(mod, "on_" + hook)
print "attaching %s.%s to %s" % (modname, hookfun, hook)
hooks.setdefault(hook, []).append(hookfun)
if mod.__doc__:
firstline = mod.__doc__.split('\n')[0]
hooks.setdefault('help', {})[modname] = firstline
hooks.setdefault('extendedhelp', {})[modname] = mod.__doc__
# bare except, because the modules could raise any number of errors
# on import, and we want them not to kill our server
except:
print "import failed on module %s, module not loaded" % plugin
print "%s" % sys.exc_info()[0]
print "%s" % traceback.format_exc()
def run_hook(hook, data, server):
responses = []
for hook in hooks.get(hook, []):
h = hook(data, server)
if h:
responses.append(h)
return responses
def get_msg():
results = q.get_messages(num_messages=1, wait_time_seconds=1, visibility_timeout=30)
sqs_token = config.get("sqs_token")
if not len(results) == 0:
for result in results:
body = json.loads(result.get_body())
user = body.get("user_name", "")
channel = "#" + body.get("channel_name", "")
msgtoken = body.get("token", "")
# ignore/delete message we sent
if user == "slackbot":
q.delete_message(result)
return ""
response = run_hook("message", body, {"config": config, "hooks": hooks})
q.delete_message(result)
if not response:
return ""
send_msg(channel, response)
def send_msg(channel, response):
username = config.get("username")
icon_url = config.get("icon_url")
webhook_token = config.get("webhook_token")
domain = config.get("domain")
url = "https://" + domain + "/services/hooks/incoming-webhook?token=" + webhook_token
for item in response:
if 'fallback' in item:
payload = {'channel': channel, 'username': username, 'icon_url': icon_url, 'attachments': response}
else:
payload = {'channel': channel, 'username': username, 'text': response, 'icon_url': icon_url}
r = requests.post(url, data=json.dumps(payload), timeout=5)
print r.status_code
if __name__ == '__main__':
init_plugins()
app = StartBot()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()