-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.py
executable file
·101 lines (85 loc) · 3.22 KB
/
App.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
#!/usr/bin/env python3
"""===============================================================================
FILE: App.py
USAGE: (not intended to be directly executed)
DESCRIPTION:
OPTIONS: ---
REQUIREMENTS: ---
BUGS: ---
NOTES: ---
AUTHOR: Alex Leontiev ([email protected])
ORGANIZATION:
VERSION: --
CREATED: 2021-01-31T17:12:33.975745
REVISION: ---
==============================================================================="""
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
from pymongo import MongoClient
import click
import json
import os
from os import path
import atexit
from py.eldrinko.el_drinko_pub_bot import ElDrinkoPubBot
import logging
from datetime import datetime
class _AtExitHook:
def __init__(self, pidfile):
self._pidfile = pidfile
def __call__(self):
click.echo("_atexit_hook")
os.system(f"rm -rf {self._pidfile}")
@click.command()
@click.option("--mongo-url", envvar="MONGO_URL")
@click.option("--template-folder", type=click.Path(), envvar="TEMPLATE_FOLDER")
@click.option("-e", "--environment", type=click.Choice(["ElDrinkoPubBot", "ProtoElDrinkoPubBot", "DevElDrinkoPubBot"]), default="DevElDrinkoPubBot", envvar="BEERBOT_ENVIRONMENT")
@click.option("--debug/--no-debug", default=True)
def App(mongo_url, environment, debug, template_folder):
pidfile = f".tmp/{environment}.txt"
basic_config_kwargs = {"handlers": [], "level": logging.DEBUG}
_handler = logging.FileHandler(
filename=f".log/{environment}_{datetime.now().strftime('%Y%m%d%H%M%S')}.log.txt",
)
_handler.setFormatter(logging.Formatter(
fmt='%(asctime)s,%(msecs)d %(levelname)-8s %(name)s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
))
_handler.setLevel(logging.DEBUG)
basic_config_kwargs["handlers"].append(_handler)
_handler = logging.StreamHandler()
if debug:
_handler.setLevel(logging.INFO)
else:
_handler.setLevel(logging.WARNING)
basic_config_kwargs["handlers"].append(_handler)
logging.basicConfig(**basic_config_kwargs)
assert not path.isfile(
pidfile), "only one instance of ElDrinkoPubBot allowed to run"
with open(pidfile, "w") as f:
f.write(str(os.getpid()))
mongo_client = MongoClient(mongo_url)
settings, keyring = [
{k: v for k, v in mongo_client.beerbot[kk].find_one(
{"id": environment}).items() if k not in ["_id", "id"]}
for kk
in ["_settings", "_keyring"]
]
atexit.register(_AtExitHook(pidfile))
click.echo("here")
updater = Updater(keyring["telegram"]["token"], use_context=True)
bot = updater.bot
edbp = ElDrinkoPubBot(
settings={**settings, "id": environment},
bot=bot,
mongo_url=mongo_url,
template_folder=template_folder
)
updater.dispatcher.add_handler(
MessageHandler(filters=Filters.all, callback=edbp))
updater.dispatcher.add_handler(
CallbackQueryHandler(callback=edbp))
updater.start_polling()
updater.idle()
if __name__ == "__main__":
App()