-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
132 lines (95 loc) · 4.07 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
import json
import logging
import sqlite3
import requests
import datetime
from telegram import (ReplyKeyboardRemove)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler)
# Bot-Token hier einfügen
token = ""
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
morgens = datetime.time(hour=7, minute=30)
KLASSE = range(1)
db = sqlite3.connect('gvtbotdata.db', check_same_thread=False)
c = db.cursor()
c.execute(
'''CREATE TABLE IF NOT EXISTS users(id INT PRIMARY KEY, username VARCHAR(64), klasse VARCHAR(8), UNIQUE(id, username))''')
db.commit()
def newUser(id, username, klasse):
c.execute('''INSERT OR IGNORE INTO users(id, username, klasse) VALUES(?,?,?)''', (id, username, klasse))
db.commit()
def start(bot, update):
user = update.message.from_user
try:
c.execute('''SELECT klasse FROM users WHERE id = ?''', (user.id,))
sendplan(bot, user.id, c.fetchone()[0])
return ConversationHandler.END
except:
update.message.reply_text('Gebe deine Klasse ein:')
return KLASSE
def klasse(bot, update):
user = update.message.from_user
newUser(user.id, user.username, update.message.text)
sendplan(bot, user.id, update.message.text)
return ConversationHandler.END
def cancel(bot, update):
update.message.reply_text('Bye', reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def delklasse(bot, update):
user = update.message.from_user
try:
c.execute('''DELETE FROM users WHERE id = ?''', (user.id,))
db.commit()
update.message.reply_text('Daten erfolgreich gelöscht')
except:
update.message.reply_text('Irgendwas ging schief')
def sendplan(bot, userid, userklasse):
try:
params = {'cert': 0}
r = requests.get('http://fbi.gruener-campus-malchow.de/cis/pupilplanapi', params=params)
vt = json.loads(json.dumps(r.json()))
bot.sendMessage(chat_id=userid,
text='Hinweis: Aus Datenschutzgründen können keine Lehrernamen angezeigt werden.')
for info in vt[0]['Informationen']:
bot.sendMessage(chat_id=userid, text='Informationen:\n\n' + info)
for n in vt[0][userklasse]:
bot.sendMessage(chat_id=userid, text='Stunde: ' + vt[0][userklasse][n]['Stunde'] + '\n' +
'Fach: ' + vt[0][userklasse][n]['Fach'] + '\n' +
# 'LehrerIn: ' + vt[0][userklasse][n]['LehrerIn'] + '\n' +
'Raum: ' + vt[0][userklasse][n]['Raum'] + '\n' +
'Art: ' + vt[0][userklasse][n]['Art'] + '\n' +
'Hinweis: ' + vt[0][userklasse][n]['Hinweis'] + '\n')
except:
bot.sendMessage(chat_id=userid,
text='Entweder ist das keine gültige Klasse, oder sie hat heute keine Vertretung.',
reply_markup=ReplyKeyboardRemove())
def updateAnAlle(bot, job):
c.execute('''SELECT * FROM users''')
for user in c.fetchall():
sendplan(bot, user[0], user[2])
def error(bot, update, error):
logger.warning('Update "%s" verursachte Fehler "%s"' % (update, error))
def main():
updater = Updater(token)
job_queue = updater.job_queue
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
KLASSE: [MessageHandler(Filters.all, klasse)]
},
fallbacks=[CommandHandler('cancel', cancel)],
allow_reentry=True
)
delklasseHandler = CommandHandler('delklasse', delklasse)
dp.add_handler(conv_handler)
dp.add_error_handler(error)
dp.add_handler(delklasseHandler)
job_queue.run_daily(updateAnAlle, morgens, days=(0, 1, 2, 3, 4))
# Startet den Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()