Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Simple echo-bot
- """
- import argparse
- from notifier import start_notifier, CHECK_FREQUENCY
- from multiprocessing import Process, Lock
- from bot_factory import BotFactory
- from data_storage import DataStorage
- from tabulate import tabulate
- import re
- def link_from_id(sheet_id):
- return f'https://docs.google.com/spreadsheets/d/{sheet_id}'
- def parse_args():
- parser = argparse.ArgumentParser()
- # parser.add_argument("config_path", help="path to the configuration file")
- parser.add_argument("token", help="telegram bot token")
- parser.add_argument("api_key", help="google sheets api key")
- args = parser.parse_args()
- return args
- if __name__ == '__main__':
- launch_args = parse_args()
- bot_factory = BotFactory(launch_args.token)
- bot = bot_factory.create_bot()
- db_path = "data"
- db_storage = DataStorage(db_path)
- db_lock = Lock()
- @bot.message_handler(commands=['set'])
- def echo_all(message):
- if len(message.text.split(' ')) != 4:
- bot.send_message(chat_id=message.chat.id, text="Wrong format")
- else:
- _, name, sheet_id, cell_range = message.text.split(' ')
- chat_id = message.chat.id
- if db_storage.is_in_db(chat_id, name, sheet_id):
- bot.reply_to(message, "Already set")
- return
- db_lock.acquire()
- db_storage.add_record(chat_id, name, sheet_id, cell_range)
- bot.reply_to(message, text="Successfully set")
- db_lock.release()
- @bot.message_handler(commands=['list'])
- def echo_all(message):
- records = db_storage.records_by_user(message.chat.id)
- headers = ["Name", "Table Link", "Cell Range"]
- data = [[rec['name'],
- "click",
- rec['range']] for rec in records]
- message_text = tabulate(data, headers=headers)
- # TODO: make code better
- click_enters = [m.start() for m in re.finditer("click", message_text)]
- for i, sheet_id in click_enters, [rec['sheet_id'] for rec in records]):
- message_text = message_text[:i] + link_from_id(sheet_id) + message_text[i+len("click"):]
- bot.send_message(chat_id=message.chat.id, text=message_text, parse_mode="HTML", disable_web_page_preview=True)
- @bot.message_handler(commands=['help'])
- def echo_all(message):
- message_text = "For setting a notification enter a message in the following format\n" \
- "\'/set <name> <google_sheet_id> <cell range>\' (each value must not contain spaces)\n" \
- "cell range format - A1 or R1C1 notation\n" \
- "for example:\n" \
- "\'/set test_results 17h7GKuJ1gS7faOiyM7dBaM1XPUHPXPgIb7zCFfFGUOE Sheet1!A1:A2\'"
- bot.send_message(chat_id=message.chat.id, text=message_text)
- @bot.message_handler(commands=['check_frequency'])
- def echo_all(message):
- bot.send_message(chat_id=message.chat.id,
- text=f"Current update frequency is {CHECK_FREQUENCY} seconds")
- # notifier_process = Process(target=start_notifier, args=(db_lock, bot_factory, db_path, launch_args.api_key))
- # notifier_process.start()
- print(f"Bot started with token{launch_args.token}")
- bot.infinity_polling()
Advertisement
Add Comment
Please, Sign In to add comment