-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
116 lines (100 loc) · 2.93 KB
/
main.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
import json
import requests
import random
hello_list = [ # from https://www.ethnolink.com.au/how-to-say-hello-in-50-different-languages/
'(Afrikaans) Goeie dag',
'(Albanian) Tungjatjeta',
'(Arabic) Ahlan bik',
'(Bengali) Nomoskar',
'(Bosnian) Selam',
'(Burmese) Mingala ba',
'(Chinese) Nín hao',
'(Croatian) Zdravo',
'(Czech) Nazdar',
'(Danish) Hallo',
'(Dutch) Hallo',
'(Filipino) Helo',
'(Finnish) Hei',
'(French) Bonjour',
'(German) Guten Tag',
'(Greek) Geia!',
'(Hebrew) Shalóm',
'(Hindi) Namasté',
'(Hungarian) Szia',
'(Indonesian) Hai',
'(Iñupiaq) Kiana',
'(Irish) Dia is muire dhuit',
'(Italian) Buongiorno',
'(Japanese) Kónnichi wa',
'(Korean) Annyeonghaseyo',
'(Lao) Sabai dii',
'(Latin) Ave',
'(Latvian) Es mīlu tevi',
'(Malay) Selamat petang',
'(Mongolian) sain baina uu',
'(Nepali) Namaste',
'(Norwegian) Hallo.',
'(Persian) Salâm',
'(Polish) Witajcie',
'(Portuguese) Olá',
'(Romanian) Salut',
'(Russian) Privét',
'(Samoan) Talofa',
'(Serbian) ćao',
'(Slovak) Nazdar',
'(Slovene) Zdravo',
'(Spanish) Hola',
'(Swahili) Jambo',
'(Swedish) Hej',
'(Tagalog) Halo',
'(Thai) Sàwàtdee kráp',
'(Turkish) Merhaba',
'(Ukrainian) Pryvít',
'(Urdu) Adaab arz hai',
'(Vietnamese) Chào',
]
config = json.loads(open('config.json', 'r').read())
def verify_web_hook(form):
if not form or form.get('token') != config['SLACK_TOKEN']:
raise ValueError('Invalid request/credentials')
def handle_request(request, response_url):
command = ""
valid_command = False
# default to returning an error statement
if (request):
commandElements = request.split()
command = commandElements[0]
params = commandElements[1:]
if (command == 'hello'):
valid_command = True
message = random.choice(hello_list)
else:
message = "hello_bot doesn't know what '{cmd}' means".format(
cmd=command)
target_url = ''
if (valid_command == True):
# Sends the response back to the channel
target_url = config['WEBHOOK_URL']
slack_data = {
'text': message
}
else:
# Sends the response back to the requester only
target_url = response_url
slack_data = {
'response_type': "ephemeral",
'text': message
}
# Send the result of the command back to Slack
response = requests.post(
target_url, data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)
def hello_bot(request):
if request.method != 'POST':
return 'Only POST requests are accepted', 405
verify_web_hook(request.form)
response_url = request.form.get('response_url')
handle_request(request.form['text'], response_url)
# return empty string instead to reduce spam
return ''