-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
167 lines (153 loc) · 4.71 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
import os, sys
from flask import Flask, request
from pymessenger import Bot
import bs4dcu
app = Flask(__name__)
PAGE_ACCESS_TOKEN = "EAADAkzBKVHABADJ7vNYyMrY5TDUOXv9OHZA3ZAXWJmpK4hzjCxIPqkLpgGLzbfpaK8vWUqjKJc4BBWqwwATwJ0xAnTeYMcZC9ZBLtB9kongYXZBdYCMYMKDmCWjF6tLBH2Jx0jIork7d0tgK8BmPmDxB2kZAZClSBNjeeGb59AFOwZDZD"
bot = Bot(PAGE_ACCESS_TOKEN)
#Time Check
def timecheck(s):
if s.lower() == "none":
return None
else:
return s
# User Functions
def isuser(sid):
with open("userinfo.txt") as file:
for x in file:
l = x.split(":")
if l[0] == sid:
return True
return False
def updateuser(sid, code, year):
with open("userinfo.txt", "r+") as file:
d = file.readlines()
file.seek(0)
for x in d:
l = x.split(":")
if l[0] != sid:
file.write(x)
file.write(sid+":"+code+":"+year+"\n")
file.truncate()
file.close
def userinfo(sid):
with open("userinfo.txt") as file:
for x in file:
l = x.split(":")
code = l[1]
year = l[2]
return code, year
@app.route('/', methods=['GET'])
def verify():
# Webhook verification
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == "hello":
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "OK", 200
@app.route('/', methods=['POST'])
def webhook():
data = request.get_json()
log(data)
response = ""
if data['object'] == 'page':
for entry in data['entry']:
for messaging_event in entry['messaging']:
# IDs
sender_id = messaging_event['sender']['id']
#recipient_id = messaging_event['recipient']['id']
#Get UserInfo
usercheck = isuser(sender_id)
if usercheck:
code, year = userinfo(sender_id)
year = year.strip()
print(code, year)
time = None
#Message Handling
if messaging_event.get('message'):
# Extracting text message
if 'text' in messaging_event['message']:
messaging_text = messaging_event['message']['text'].lower()
print("Got Text Message!")
#!next Event
if messaging_text[:4] == "next":
x = messaging_text
x = x.split()
if len(x) > 2:
code = x[1].upper()
year = x[2]
response = bs4dcu.next(code, year)
elif usercheck:
response = bs4dcu.next(code, year)
if response == "":
response = "Nothing On Next"
#!today Event
elif messaging_text[:5] == "today":
x = messaging_text
x = x.split()
if len(x) > 2:
code = x[1].upper()
year = x[2]
response = bs4dcu.gettoday(code, year)
response = bs4dcu.dictionaryhandler(response)
elif usercheck:
response = bs4dcu.gettoday(code, year)
response = bs4dcu.dictionaryhandler(response)
#ON Event
elif messaging_text[:2] == "on":
try:
x = messaging_text
x = x.split()
#Search
if len(x) == 5:
print("Running This!...")
day = x[1].title()
time = timecheck(x[2])
code = x[3].upper()
year = x[4]
response = bs4dcu.run(day, time, code, year)
if time == None:
response = bs4dcu.dictionaryhandler(response)
else:
response = bs4dcu.responsehandler(response)
#User - Day Given
elif usercheck and len(x) == 2:
day = x[1].title()
response = bs4dcu.run(day, None, code, year)
response = bs4dcu.dictionaryhandler(response)
#User - Time Given
elif usercheck and len(x) == 3:
day = x[1].title()
time = x[2]
response = bs4dcu.run(day, time, code, year)
response = bs4dcu.responsehandler(response)
else:
response = "Unsupported On Attributes"
except:
response = "Error Running On Command :("
#!config Event
elif messaging_text[:3] == "set":
x = messaging_text.split()
if len(x) == 3:
code = x[1].upper()
year = x[2]
if not usercheck:
with open("userinfo.txt", "a") as file:
file.write(sender_id+":"+code+":"+year+"\n")
response = "Set your config! :)"
else:
updateuser(sender_id, code, year)
response = "Updated your config! :)"
else:
response = "Format should be !config CODE YEAR"
else:
response = 'Nothing Received...'
# Echo
# response = messaging_text
bot.send_text_message(sender_id, response)
return "OK", 200
def log(message):
print(message)
sys.stdout.flush()
if __name__ == "__main__":
app.run(debug = True, port = 80)