-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
74 lines (49 loc) · 2.17 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
import os
import json
import logging
import logging.config
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import Filters
from config import config
from jobs import JOBS_CALLBACKS
import utils as u
def load_logging_config(config_file_path='logging.json'):
with open(config_file_path, 'r') as f:
logging_config = json.load(f)
logging.config.dictConfig(logging_config)
logger = logging.getLogger(__name__)
load_logging_config()
@u.restricted
def delete_downloads(_, update):
logger.info('cleaning download dir')
files = [f for f in os.listdir('downloads/') if f != '.gitkeep']
for f in files:
os.remove(os.path.join('downloads', f))
update.message.reply_text('Deleted {} files'.format(len(files)))
@u.restricted
def send_db(_, update):
logger.info('sending_db')
with open(config.database.filename, 'rb') as f:
update.message.reply_document(f)
@u.restricted
def help_command(_, update):
logger.info('help')
commands = ['/del', '/db', '/start']
update.message.reply_text('Commands: {}'.format(', '.join(commands)))
def main():
updater = Updater(token=config.telegram.token, workers=config.telegram.run_async_workers)
dispatcher = updater.dispatcher
jobs = updater.job_queue
logger.info('registering %d scheduled jobs', len(JOBS_CALLBACKS))
for callback in JOBS_CALLBACKS:
jobs.run_repeating(callback, interval=config.jobs.run_every, first=config.jobs.start_after)
# dispatcher.add_handler(MessageHandler(~Filters.private & ~Filters.group & Filters.text, on_channel_post))
dispatcher.add_handler(CommandHandler(['del'], delete_downloads, filters=Filters.private))
dispatcher.add_handler(CommandHandler(['db'], send_db, filters=Filters.private))
dispatcher.add_handler(CommandHandler(['start', 'help'], help_command, filters=Filters.private))
logger.info('starting polling loop as @%s (run_async workers: %d)...', updater.bot.username, config.telegram.run_async_workers)
updater.start_polling(clean=False)
updater.idle()
if __name__ == '__main__':
main()