-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_testmode.py
65 lines (45 loc) · 1.41 KB
/
bot_testmode.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
"""
Used to run the bot in longpolling mode locally.
SQLite in-memory database is in use.
"""
import os
import logging
import telebot
from flask import Flask
import models
botlogger = logging.getLogger('botlogger')
TOKEN = os.environ.get('TOKEN')
URL = os.environ.get('URL')
DATABASE_URL = os.environ.get('DATABASE_URL')
if not TOKEN:
botlogger.warning('TOKEN should be defined as system var')
if not URL:
botlogger.warning('URL should be defined as system var')
# connecting to database
creds, host_and_database = DATABASE_URL[11:].split('@')
host_and_port, database = host_and_database.split('/')
host, port = host_and_port.split(':')
user, password = creds.split(':')
models.db.bind(provider='postgres', user=user, password=password,
host=host, port=port, database=database)
models.db.generate_mapping(create_tables=True)
botlogger.info('Starting database...')
import models # noqa E402
# Setting bot
bot = telebot.TeleBot(TOKEN)
tblogger = telebot.logger
telebot.logger.setLevel(logging.INFO)
# Setting Flask
app = Flask(__name__)
# registering into flask
app.config['TELEBOT'] = bot
app.config['TELEBOT_LOGGER'] = tblogger
# registering bot handlers
with app.app_context():
from handlers import BotHandlers
BotHandlers.register()
def run_long_polling():
botlogger.info('Starting polling...')
bot.infinity_polling(skip_pending=True)
if __name__ == '__main__':
run_long_polling()