Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
64 lines (45 loc) · 2.16 KB
/
Copy pathbot.py
File metadata and controls
64 lines (45 loc) · 2.16 KB
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
# -*- coding: utf-8 -*-
import logging
import logging.handlers
import pathlib
from telegram.ext import Updater, JobQueue
import config
from blackjackbot import handlers, error_handler
from blackjackbot.gamestore import GameStore, STALE_TIMEOUT_MIN
from blackjackbot.lang import translate
from database import Database
logdir_path = pathlib.Path(__file__).parent.joinpath("logs").absolute()
logfile_path = logdir_path.joinpath("bot.log")
if not logdir_path.exists():
logdir_path.mkdir()
logfile_handler = logging.handlers.WatchedFileHandler(logfile_path, "a", "utf-8")
loglevels = {"debug": logging.DEBUG, "error": logging.DEBUG, "fatal": logging.FATAL, "info": logging.INFO}
loglevel = loglevels.get(config.LOGLEVEL, logging.INFO)
logger = logging.getLogger(__name__)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=loglevel, handlers=[logfile_handler, logging.StreamHandler()])
logging.getLogger("telegram").setLevel(logging.ERROR)
logging.getLogger("apscheduler").setLevel(logging.ERROR)
updater = Updater(token=config.BOT_TOKEN, use_context=True)
for handler in handlers:
updater.dispatcher.add_handler(handler)
updater.dispatcher.add_error_handler(error_handler)
# Set up jobs
def stale_game_cleaner(context):
gs = GameStore()
removed_chat_ids = gs.cleanup_stale_games()
for chat_id in removed_chat_ids:
text = translate("game_ended_inactive", Database().get_lang_id(chat_id)).format(STALE_TIMEOUT_MIN)
try:
context.bot.send_message(chat_id=chat_id, text=text)
except Exception as e:
logger.warning("Couldn't notify chat {} about its stale game: {}".format(chat_id, e))
updater.job_queue.run_repeating(callback=stale_game_cleaner, interval=300, first=300)
if config.USE_WEBHOOK:
updater.start_webhook(listen=config.WEBHOOK_IP, port=config.WEBHOOK_PORT, url_path=config.BOT_TOKEN, cert=config.CERTPATH, webhook_url=config.WEBHOOK_URL)
updater.bot.set_webhook(config.WEBHOOK_URL)
logger.info("Started webhook server!")
else:
updater.start_polling()
logger.info("Started polling!")
logger.info("Bot started as @{}".format(updater.bot.username))
updater.idle()