-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
executable file
·104 lines (84 loc) · 3.09 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
#!/usr/bin/env python3
import os
import json
import requests
from importlib import reload # for loading in all modules
import re
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from flask import Flask, request
from modules.bachelor import bachelor_handler
from modules.daily_cal import daily_cal_handler
from modules.detain import detain_handler
from modules.dierre_pics import dierre_pic_handler
from modules.door import door_handler
from modules.noise_complaint import noise_complaint_handler
from modules.party import party_handler
from modules.sign_in import sign_in_handler
from modules.usage import usage_handler
from modules.weather import weather_handler
from modules.penny import penny_pic_handler
from modules.tethics_dare import dare_handler
# Globs
app = Flask(__name__)
# get bot_id and api keys
with open(".secret", "r") as f:
secrets = json.loads(f.read())
bot_id = secrets['bot_id']
app_id = secrets['appid']
def call_handler(sender, message, bot_id, app_id):
"""Route commands to the correct module function"""
command = message.split()[0]
methods = {
'!attendance': sign_in_handler,
'!bachelor': bachelor_handler,
'!calendar': daily_cal_handler,
'!detain': detain_handler,
'!dierre': dierre_pic_handler,
'!door': door_handler,
'!party': party_handler,
'!roseceremony': bachelor_handler,
'!usage': usage_handler,
'!weather': weather_handler,
'!penny': penny_pic_handler,
'!dare': dare_handler
}
# Get the function from handlers dictionary, add message as argument, return None on KeyError
handler = methods.get(command)
# Execute the function
if handler: return handler(sender, message, bot_id, app_id)
return None
@app.route('/botjawn', methods=['POST'])
def shortstop():
"""Handle requests from GroupMe"""
sender = request.get_json()['name']
message = request.get_json()['text']
# Log message
print(message)
# Bot logic
if message and len(message) > 0 and message[0] == '!':
# Get response from handler, if it's a valid command
res = call_handler(sender, message, bot_id, app_id)
if res: reply(res)
# Check if noise complaint in order
elif message and message == message.upper() and len(message) > 0 and re.search('[A-Z]', message):
reply(noise_complaint_handler(sender, message, bot_id, app_id))
print('INCOMING REQUEST FROM {}'.format(request.remote_addr))
return "ok", 200
def reply(msg):
"""Reply to a message in the chat"""
url = 'https://api.groupme.com/v3/bots/post'
if msg == None or msg == "None": #not sure when the cast to str happens.
return None
data = {
'bot_id' : bot_id,
'text' : msg
}
request = Request(url, urlencode(data).encode())
json = urlopen(request).read().decode()
def sender_is_bot(message):
"""Check if sender is bot to not reply to own msgs"""
return message['sender_type'] == "bot"
if __name__ == '__main__':
# Run on raspi's local ip addr
app.run(host='192.168.1.214')