def remove_app(update, context): buttons = [] for app_id in db.get_apps_for_account(context.user_data['managed_account']): buttons.append(button(str(app_id), f'remove_{app_id}')) buttons.append(back_btn) reply(update, context, manage_acc_texts['remove_app'], buttons) return 12
def do_add_app(update, context): try: app_id = int(update.effective_message.text) except ValueError: reply(update, context, manage_acc_texts['nan'], [back_btn]) return 11 db.add_app(app_id, context.user_data['managed_account']) return manage_account_menu(update, context)
def get_app_id(update, context): try: app_id = int(update.effective_message.text) except ValueError: reply(update, context, texts['nan'], [back_btn]) return 1 if not (app_account := db.get_app_account(app_id)): reply(update, context, texts['app_not_found'], [back_btn]) return 1
def admin_main(update, context): context.user_data.pop('new_account', None) context.user_data.pop('managed_account', None) buttons = [add_acc_btn] for account_id, login in db.get_accounts(): buttons.append(button(login, f'manage_{account_id}')) buttons.append(back_btn) reply(update, context, admin_main_text, buttons) return 1
def get_ad_account_id(update, context): try: ad_account_id = int(update.effective_message.text) except ValueError: reply(update, context, texts['nan'], [back_btn]) return 2 connect_data = context.user_data['connect_accounts'] connect_data['ad_accounts'].add(ad_account_id) message_text = texts['finish'].format( connect_data['app_id'], '\n'.join(str(ad_acc_id) for ad_acc_id in connect_data['ad_accounts'])) reply(update, context, message_text, [confirm_btn, back_btn]) return 3
def connect(update, context): update.effective_message.edit_text(texts['working'], reply_markup=None) connect_data = context.user_data.pop('connect_accounts') app_id, ad_accounts = connect_data['app_id'], connect_data['ad_accounts'] try: connected_accounts = facebook.update_app(**connect_data) except Exception as err: # pylint: disable=broad-except message_text = texts['result_fail'] logging.error("Ad accounts connection error: %s", err) else: not_connected = ad_accounts.difference(connected_accounts) count = len(ad_accounts) - len(not_connected) message_text = texts['result_success'].format(app_id, count) if not_connected: message_text += texts['result_partial'].format('\n'.join( str(missing_id) for missing_id in not_connected)) if count: logging.info("New ad accounts (%s) connected to app %s", count, app_id) reply(update, context, message_text, [back_btn]) return 1
def manage_account_menu(update, context): reply(update, context, manage_acc_texts['main'], manage_acc_buttons) return 1
def get_password(update, context): new_account = context.user_data['new_account'] new_account['password'] = update.effective_message.text message = add_acc_texts['confirm'].format(**new_account) reply(update, context, message, [confirm_btn, back_btn]) return 3
def get_login(update, context): login = update.effective_message.text context.user_data['new_account']['login'] = login message = add_acc_texts['ask_password'].format(login) reply(update, context, message, [back_btn]) return 2
def add_account(update, context): context.user_data['new_account'] = {} reply(update, context, add_acc_texts['ask_login'], [back_btn]) return 1
def delete_account(update, context): message = manage_acc_texts['delete_account'] reply(update, context, message, [confirm_btn, back_btn]) return 14
def change_password(update, context): reply(update, context, manage_acc_texts['change_pass'], [back_btn]) return 13
def add_app(update, context): reply(update, context, manage_acc_texts['add_app'], [back_btn]) return 11
def start(update, context): reply(update, context, texts['app_id'], [back_btn]) return 1
def get_app_id(update, context): try: app_id = int(update.effective_message.text) except ValueError: reply(update, context, texts['nan'], [back_btn]) return 1 if not (app_account := db.get_app_account(app_id)): reply(update, context, texts['app_not_found'], [back_btn]) return 1 context.user_data['connect_accounts'] = { 'app_id': app_id, 'app_account': app_account, 'ad_accounts': set() } reply(update, context, texts['ad_account'].format(app_id), [back_btn]) return 2 def get_ad_account_id(update, context): try: ad_account_id = int(update.effective_message.text) except ValueError: reply(update, context, texts['nan'], [back_btn]) return 2 connect_data = context.user_data['connect_accounts'] connect_data['ad_accounts'].add(ad_account_id) message_text = texts['finish'].format( connect_data['app_id'], '\n'.join(str(ad_acc_id) for ad_acc_id in connect_data['ad_accounts'])) reply(update, context, message_text, [confirm_btn, back_btn])
def start(update, context): reply(update, context, main_menu_text, [connect_ad_account_btn])