-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
150 lines (112 loc) · 3.33 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import requests
import time
import random
import datetime
from datetime import datetime
from threading import Timer
from bs4 import BeautifulSoup
from flask import Flask, request
app = Flask(__name__)
@app.route("/", methods=["GET"])
def home():
return "Where my bot for Montgomery Hall lives :) - Jason, implementing SSL soon"
# print() are in the log file
@app.route("/", methods=["POST"])
def receive():
print("Incoming message:")
data = request.get_json()
print(data)
# prevent self-reply
if data["sender_type"] != "RA BOT":
read(data["text"].lower())
if data["text"].startswith("/ping"):
send(data["name"] + " pinged me!")
return "ok", 200
# happens everyday, using the timer at the bottom
def update():
getFood()
goodMorning()
def goodMorning():
send("Good Morning Montgomery Hall")
def showMenu():
return """RA BOT MENU: \n
You can send the bot a command! \n
Upcoming Events: /events \n
Is Jason on Duty right now?: /duty? \n
Pick a num between 1-10: /pick \n
Flip a coin: /flip \n
Phone Numbers: /numbers \n"""
def readEvents():
with open("events.txt") as f:
send("".join(f.readlines()))
def readFood():
with open("food.txt") as f:
send("".join(f.readlines()))
# scrapes data from umd dining menu
def getFood():
page = requests.get("http://nutrition.umd.edu/") # not secured smh
soup = BeautifulSoup(page.content, "html.parser")
elements = soup.find_all("a")
food = set()
for i in range(12, len(elements)):
item = str(elements[i])
item = item[item.find(">") + 1 :].replace(
"</a>", ""
) # locate food, and remove tags
# don't add the leftover tags
if not (
item.startswith(" <span") or item.startswith("(") or item.startswith("http")
):
food.add(item)
f = open("food.txt", "w")
for item in food:
f.write(item + "\n")
return food
def chicken_tenders(food):
if "Chicken Tenders" in food:
return True
return False
def read(msg):
text = msg.split(" ")
if msg == "/menu":
send(showMenu())
elif msg == "/events":
send(readEvents())
elif msg == "/duty?":
# pulls data from Google Calendar API
send("No")
elif msg == "/pick":
send(str(random.randint(1, 10)))
elif msg == "/flip":
toss = random.randint(1, 2)
if toss == 1:
send("tails")
else:
send("heads")
elif msg == "/numbers":
# numbers() -> not shown for sake of confidentality
send(numbers)
elif msg == "/food":
readFood()
elif msg == "/tendies":
food = getFood()
if chicken_tenders(food):
send("CHICKEN TENDERS @ 12")
else:
send("Sorry, not today")
return True
def send(msg):
url = "https://api.groupme.com/v3/bots/post"
data = {
"bot_id": os.getenv("GROUPME_BOT_ID"),
"text": msg,
}
# sends message to GroupMe
r = requests.post(url, json=data)
while True:
present = datetime.today()
nextDay = present.replace(day=present.day + 1, hour=6, minute=0, second=0)
changeTime = nextDay - present
t = Timer(changeTime, update())
t.start()