This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
128 lines (93 loc) · 3.71 KB
/
run.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
from telegram.ext import MessageHandler, Filters, Updater
from telegram.error import (TelegramError, Unauthorized, BadRequest,
TimedOut, ChatMigrated, NetworkError)
import telegram.ext
import parser
import time
import datetime
import os
import logging
from configparser import ConfigParser
logging.basicConfig(filename='optimat.log', level=logging.INFO)
config = ConfigParser()
config.read('config.ini')
# this is the handler to answer incoming chat requests
p = parser.Parser()
updater = Updater(token=config.get(
'main', 'TELEGRAM_BOT_TOKEN'), use_context=True)
scheduler = updater.job_queue
dispatcher = updater.dispatcher
# this handler reacts on messages sent to the bot
def error_callback(update, context):
try:
raise context.error
except TimedOut:
logging.exception("timeout error")
except NetworkError:
logging.exception("Network error")
def optimatHandler(update, context):
try:
result = p.parseInput(update.message.text)
updater.bot.sendMessage(config.get(
'main', 'MY_TELEGRAM_ID'), result['reply'])
except Exception:
logging.exception("Handlig telegram message failed")
optimat = MessageHandler(Filters.text, optimatHandler)
dispatcher.add_handler(optimat)
dispatcher.add_error_handler(error_callback)
# this handler is triggered via scheduler every 5min to update the dashboard in the kitchen
def callback_updateDashboard(context):
try:
p.updateDashboard()
except Exception:
logging.exception("Update of the dashboard server failed")
dashboardUpdateJob = scheduler.run_repeating(
callback_updateDashboard, interval=300, first=1)
# scheduled chats, e.g. send traffic in the morning or the sbahn in the evening after work
def callback_sendTrain(context):
try:
result = p.parseInput('sbahn')
updater.bot.sendMessage(config.get(
'main', 'MY_TELEGRAM_ID'), result['reply'])
except Exception:
logging.exception("Scheduled train info could not be sent")
def callback_sendQuote(context):
try:
#result = p.parseInput('Thema test')
p.saveRandomMotd()
# updater.bot.sendMessage(config.get(
# 'main', 'MY_TELEGRAM_ID'), result['reply'])
except Exception:
logging.exception("Scheduled quote info could not be sent")
def callback_triggerFunction(context):
try:
#result = p.parseInput('Thema test')
logging.info("Triggered azure function")
# stopped due to migration from telegram to signal : p.triggerFunction()
# updater.bot.sendMessage(config.get(
# 'main', 'MY_TELEGRAM_ID'), result['reply'])
except Exception:
logging.exception("Scheduled function could not be sent")
# def callback_sendCorona(context):
# try:
#result = p.parseInput('Thema test')
# p.getCorona()
# updater.bot.sendMessage(config.get(
# 'main', 'MY_TELEGRAM_ID'), result['reply'])
# except Exception:
# logging.exception("Scheduled corona info could not be sent")
# TrainUpdateJob = scheduler.run_daily(
# callback_sendTrain, datetime.time(hour=22, minute=27, second=0))
QuoteUpdateJob = scheduler.run_daily(
callback_sendQuote, datetime.time(hour=20, minute=20, second=0))
FunctionUpdateJob = scheduler.run_daily(
callback_triggerFunction, datetime.time(hour=4, minute=30, second=0), days=tuple(range(5)))
# CoronaUpdateJob = scheduler.run_daily(
# callback_sendCorona, datetime.time(hour=6, minute=20, second=0))
logging.info("Started Optimat at")
logging.info(datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
# This checks for new messages
try:
updater.start_polling()
except:
logging.exception("Telegram polling failed")