-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
82 lines (71 loc) · 2.23 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import telegram
from telegram.error import NetworkError, Unauthorized, TimedOut
from time import sleep
import os
from time import time
from src.entry import entry, syncSheetData, user_log
from src.gsheets_main import upload_to_sheets
import sys
import traceback
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
try:
BOT_TOKEN = os.environ["BOT_TOKEN"]
except KeyError:
logging.error("Bot credentials not found in environment")
sys.exit("End program with error")
# How long the container exist
LIFESPAN = 7200
def main():
"""Run the bot."""
syncSheetData()
try:
update_id = int(os.environ["UPDATE_ID"])
except:
update_id = 0
start_time = int(time())
bot = telegram.Bot(BOT_TOKEN)
while True:
try:
for update in bot.get_updates(offset=update_id, timeout=10):
update_id = update.update_id + 1
logging.info(f"Update ID:{update_id}")
entry(bot, update)
except NetworkError as e:
print("Network Error")
print(e)
traceback.print_exc()
# logging.error(update)
sleep(1)
except Unauthorized:
print("Unauthorized")
logging.error(update)
# The user has removed or blocked the bot.
# update_id += 1
except TimedOut:
logging.error("Timeout")
traceback.print_exc()
sleep(5)
except Exception as e:
logging.error("Generic Error")
logging.error(e)
logging.info("uploading pending stuff")
upload_to_sheets(user_log)
traceback.print_exc()
sleep(5)
sys.exit("End program with error")
if int(time()) - start_time > LIFESPAN:
logging.info("uploading pending stuff")
upload_to_sheets(user_log)
logging.info("Enough for the day! Passing on to next Meeseek")
with open("/tmp/update_id", "w") as the_file:
the_file.write(str(update_id))
break
if __name__ == "__main__":
main()