TrickmanOff

te

Dec 23rd, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. """
  2. Simple echo-bot
  3. """
  4. import argparse
  5.  
  6. from notifier import start_notifier, CHECK_FREQUENCY
  7. from multiprocessing import Process, Lock
  8.  
  9. from bot_factory import BotFactory
  10. from data_storage import DataStorage
  11.  
  12. from tabulate import tabulate
  13.  
  14. import re
  15.  
  16. def link_from_id(sheet_id):
  17. return f'https://docs.google.com/spreadsheets/d/{sheet_id}'
  18.  
  19.  
  20. def parse_args():
  21. parser = argparse.ArgumentParser()
  22. # parser.add_argument("config_path", help="path to the configuration file")
  23. parser.add_argument("token", help="telegram bot token")
  24. parser.add_argument("api_key", help="google sheets api key")
  25. args = parser.parse_args()
  26. return args
  27.  
  28.  
  29. if __name__ == '__main__':
  30. launch_args = parse_args()
  31.  
  32. bot_factory = BotFactory(launch_args.token)
  33. bot = bot_factory.create_bot()
  34.  
  35. db_path = "data"
  36. db_storage = DataStorage(db_path)
  37. db_lock = Lock()
  38.  
  39. @bot.message_handler(commands=['set'])
  40. def echo_all(message):
  41. if len(message.text.split(' ')) != 4:
  42. bot.send_message(chat_id=message.chat.id, text="Wrong format")
  43. else:
  44. _, name, sheet_id, cell_range = message.text.split(' ')
  45. chat_id = message.chat.id
  46.  
  47. if db_storage.is_in_db(chat_id, name, sheet_id):
  48. bot.reply_to(message, "Already set")
  49. return
  50.  
  51. db_lock.acquire()
  52. db_storage.add_record(chat_id, name, sheet_id, cell_range)
  53. bot.reply_to(message, text="Successfully set")
  54. db_lock.release()
  55.  
  56.  
  57. @bot.message_handler(commands=['list'])
  58. def echo_all(message):
  59. records = db_storage.records_by_user(message.chat.id)
  60.  
  61. headers = ["Name", "Table Link", "Cell Range"]
  62. data = [[rec['name'],
  63. "click",
  64. rec['range']] for rec in records]
  65. message_text = tabulate(data, headers=headers)
  66.  
  67. # TODO: make code better
  68. click_enters = [m.start() for m in re.finditer("click", message_text)]
  69. for i, sheet_id in click_enters, [rec['sheet_id'] for rec in records]):
  70. message_text = message_text[:i] + link_from_id(sheet_id) + message_text[i+len("click"):]
  71.  
  72. bot.send_message(chat_id=message.chat.id, text=message_text, parse_mode="HTML", disable_web_page_preview=True)
  73.  
  74. @bot.message_handler(commands=['help'])
  75. def echo_all(message):
  76. message_text = "For setting a notification enter a message in the following format\n" \
  77. "\'/set <name> <google_sheet_id> <cell range>\' (each value must not contain spaces)\n" \
  78. "cell range format - A1 or R1C1 notation\n" \
  79. "for example:\n" \
  80. "\'/set test_results 17h7GKuJ1gS7faOiyM7dBaM1XPUHPXPgIb7zCFfFGUOE Sheet1!A1:A2\'"
  81. bot.send_message(chat_id=message.chat.id, text=message_text)
  82.  
  83.  
  84. @bot.message_handler(commands=['check_frequency'])
  85. def echo_all(message):
  86. bot.send_message(chat_id=message.chat.id,
  87. text=f"Current update frequency is {CHECK_FREQUENCY} seconds")
  88.  
  89.  
  90. # notifier_process = Process(target=start_notifier, args=(db_lock, bot_factory, db_path, launch_args.api_key))
  91. # notifier_process.start()
  92.  
  93. print(f"Bot started with token{launch_args.token}")
  94. bot.infinity_polling()
  95.  
Advertisement
Add Comment
Please, Sign In to add comment