-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
208 lines (159 loc) · 4.79 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python3
"""
Flask & SocketIO based webapp that connects with a Slack workspace
and carries messages between the two
(hayk (owner) talks from Slack, user talks from webapp)
"""
import os
from flask import Flask, render_template, request, Response
from flask_cors import CORS
from flask_socketio import SocketIO, join_room, leave_room
from slackclient import SlackClient
#####################
# #
# slack #
# setup #
# #
#####################
# auth token
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')
slack_client = SlackClient(SLACK_TOKEN)
# outgoing webhook token
SLACK_WEBHOOK_SECRET = os.environ.get('SLACK_WEBHOOK_SECRET')
# array of channels hayk has messaged in
hayk_channels = []
def slack_send_message(channel_id, message):
'''
Sends a message to the specified channel from "rando"
Arguments:
channel_id {str}
message {str}
'''
slack_client.api_call(
"chat.postMessage",
channel=channel_id,
text=message,
username="rando",
icon_emoji=':taco:'
)
def slack_create_channel(channel_id):
'''
Creates a channel with name: channel_id
Arguments:
channel_id {str}
'''
slack_client.api_call(
"channels.create",
name='#' + str(channel_id)
)
#####################
# #
# end #
# slack #
# setup #
# #
#####################
# flask app init
app = Flask(__name__)
app.config.update(dict(DEBUG=True))
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
# Setup cors headers to allow all domains
CORS(app)
# socketio init
socketio = SocketIO(app)
#####################
# #
# flask #
# routes #
# #
#####################
@app.route("/")
def index():
return render_template('index.html')
@app.route('/slack', methods=['POST'])
def inbound():
if request.form.get('token') == SLACK_WEBHOOK_SECRET:
username = request.form.get('user_name')
# prevent bot from sending its own messages over and over
if username == 'hayk':
channel = request.form.get('channel_name')
text = request.form.get('text')[1:]
print('hayk_channels: ' + str(hayk_channels))
print('channel: ' + str(channel))
# if hayk has never responded in this channel
if channel not in hayk_channels:
hayk_channels.append(channel)
# emit a conection message
socketio.emit(
'message',
{
'type': 'connection',
'usr': 'hayk',
'msg': ' has entered the room'
}, room=channel)
inbound_message = username + " in " + channel + " says: " + text
print(inbound_message)
sender = {'usr': username, 'channel': channel, 'msg': text}
send_message(sender)
return Response(), 200
#####################
# #
# end #
# flask #
# routes #
# #
#####################
#####################
# #
# socketio #
# handlers #
# #
#####################
@socketio.on('joined')
def joined():
# cut room name length to 21 chars (slack channel name length limit)
room = request.sid[:20]
print(room + ' connected')
join_room(room)
# create a channel with 'name': room
slack_create_channel(room)
# notify hayk on slack that someone has joined
slack_send_message(room, "Someone's here!")
# emit a conection message
socketio.emit(
'message',
{
'type': 'connection',
'usr': room[-4:],
'msg': ' has entered the room'
}, room=room)
@socketio.on('disconnect')
def disconnect():
room = request.sid[:20]
leave_room(room)
hayk_channels.remove(room)
print('Client disconnected')
@socketio.on('send_message')
def send_message(message):
# if 'usr' not specified (ie message sent by user from web app)
if 'usr' not in message.keys():
room = request.sid[:20]
usr = room[-4:]
slack_send_message(room, message['msg'])
# else sent by hayk from slack
else:
room = message['channel']
usr = message['usr']
sender = {'type': 'new_message', 'usr': usr,
'msg': message['msg'], 'room': room}
print('sender: ' + str(sender))
socketio.emit('message', sender, room=room)
#####################
# #
# end #
# socketio #
# handlers #
# #
#####################
if __name__ == "__main__":
socketio.run(app)