|
| 1 | +import json |
| 2 | +import telegram |
| 3 | +from telegram.ext import Updater |
| 4 | +from telegram.utils.helpers import escape_markdown |
| 5 | +from helper.logger import * |
| 6 | +import math |
| 7 | + |
| 8 | +def get_telegram_info(): |
| 9 | + try: |
| 10 | + with open('ms_rewards_telegram_bot.json', 'r') as f: |
| 11 | + return json.load(f) |
| 12 | + except FileNotFoundError as e: |
| 13 | + logging.exception(msg=f'Telegram updates are enabled, but ms_rewards_telegram_bot.json not found.', exc_info=False) |
| 14 | + except Exception as e: |
| 15 | + logging.exception(msg=f'Telegram updates are enabled, but failed: {e}.') |
| 16 | + |
| 17 | +def get_redeem_options(): |
| 18 | + with open('ms_rewards_redeem_options.json', 'r') as f: |
| 19 | + return json.load(f) |
| 20 | + |
| 21 | +def markdown_escape(message_text): |
| 22 | + #Remove special characters when passing exceptions to telegram. |
| 23 | + escaped_markdown = escape_markdown(message_text, version=2) |
| 24 | + return escaped_markdown |
| 25 | + |
| 26 | +def telegram_update(message_text): |
| 27 | + try: |
| 28 | + telegram_chats = get_telegram_info() |
| 29 | + for chat in telegram_chats: |
| 30 | + telegram_apikey = chat["telegram_apikey"] |
| 31 | + telegram_chatid = chat["telegram_chatid"] |
| 32 | + bot = telegram.Bot(token=telegram_apikey) |
| 33 | + bot.send_message(chat_id=telegram_chatid, text=(message_text), parse_mode="MarkdownV2") |
| 34 | + except TypeError: |
| 35 | + logging.exception(msg=f'Telegram updates are enabled, but failed to process ms_rewards_telegram_bot.json.', exc_info=False) |
| 36 | + except FileNotFoundError: |
| 37 | + logging.exception(msg=f'Telegram updates are enabled, but ms_rewards_telegram_bot.json not found.', exc_info=False) |
| 38 | + except Exception as e: |
| 39 | + logging.exception(msg=f'Telegram updates are enabled but failed to send.', exc_info=True) |
| 40 | + |
| 41 | +def flag_checkbox(done_flag): |
| 42 | + if done_flag: |
| 43 | + return '☒' |
| 44 | + else: |
| 45 | + return '☐' |
| 46 | + |
| 47 | +def points_credit_value(points): |
| 48 | + try: |
| 49 | + redeem_options = get_redeem_options() |
| 50 | + message_list = [] |
| 51 | + for option in redeem_options: |
| 52 | + available_value = str(math.floor(int(points)/int(option["price"]))*int(option["value"])) |
| 53 | + message_list.append(f'{option["currency"]}{available_value} {option["short_desc"]}') |
| 54 | + |
| 55 | + return '('+', '.join(message_list)+')' |
| 56 | + except FileNotFoundError as e: |
| 57 | + logging.info(msg=f'Skipped redemption value check, ms_rewards_redeem_options.json not found.', exc_info=False) |
| 58 | + return '' |
| 59 | + pass |
| 60 | + except Exception as e: |
| 61 | + logging.info(msg=f'Skipped redemption value check, failed to process file.', exc_info=False) |
| 62 | + return '' |
| 63 | + pass |
| 64 | + |
| 65 | +def telegram_update_post_search(email,summary): |
| 66 | + email = markdown_escape(email) |
| 67 | + pc_flag=flag_checkbox(summary.pc_search_done) |
| 68 | + pc_c=summary.pc_search_progress |
| 69 | + pc_m=summary.pc_search_max |
| 70 | + mo_flag=flag_checkbox(summary.mob_search_done) |
| 71 | + mo_c=summary.mobile_search_progress |
| 72 | + mo_m=summary.mobile_search_max |
| 73 | + pu_flag=flag_checkbox(summary.punch_card_done) |
| 74 | + pu_c=summary.punch_card_progress |
| 75 | + pu_m=summary.punch_card_max |
| 76 | + qz_flag=flag_checkbox(summary.quiz_done) |
| 77 | + qz_c=summary.quiz_progress |
| 78 | + qz_m=summary.quiz_max |
| 79 | + points_credit_value_list=markdown_escape(points_credit_value(summary.available_points)) |
| 80 | + |
| 81 | + telegram_message = ( |
| 82 | + f'Update for {email}\n' \ |
| 83 | + f'```\n' \ |
| 84 | + f' {pc_flag} PC {pc_c}/{pc_m} {pu_flag} Punch Card {pu_c}/{pu_m}\n' \ |
| 85 | + f' {mo_flag} Mob {mo_c}/{mo_m} {qz_flag} Quiz {qz_c}/{qz_m}\n' \ |
| 86 | + f'```' \ |
| 87 | + f'Total Points: {summary.available_points:,} {points_credit_value_list}' \ |
| 88 | + ) |
| 89 | + telegram_update(telegram_message) |
0 commit comments