def add_voucher_to_user_wallet(voucher_info, user_id, username): expiry_date = TimeManager.gen_expiry_date() voucher_id_in_db = voucher_info[2] voucher_to_be_added_into_db = { 'telegram_id': user_id, 'asset_type': 'coupon', 'asset_id': voucher_id_in_db, 'asset_value': 1, 'expiry_date': expiry_date } result = UserManager.add_user_voucher(voucher_to_be_added_into_db) logging_message = f"{voucher_info[0]} voucher to user {username} ({user_id})'s wallet!" if result: LoggerManager.general_logs(f"Successfully added {logging_message}") voucher_id_to_be_updated = voucher_info[7] VoucherManager.update_voucher_details(voucher_id_to_be_updated, 'dailycoupons', 'issueamount') return True else: LoggerManager.exception_logs( f"Error occurred while adding {logging_message}")
def give_reward_base_on_type(reward_of_the_day, user_id, username): logging_message = f"Updating user {username} ({user_id})'s wallet" response_result = False if reward_is_voucher(reward_of_the_day): voucher_info = reward_of_the_day LoggerManager.general_logs( f"{logging_message}. Adding voucher {voucher_info[0]} into the wallet") response_result = SharedFunctions.add_voucher_to_user_wallet(voucher_info, user_id, username) elif reward_is_snapcoin(reward_of_the_day): snapcoin_amount = reward_of_the_day LoggerManager.general_logs( f"{logging_message}. Adding {snapcoin_amount} snapcoins into the wallet") response_result = SharedFunctions.add_coin_to_user_wallet(snapcoin_amount, '+', user_id, username) else: LoggerManager.exception_logs( f"Unknown reward. Error occurred somewhere") return response_result
def broadcast_message_to_users(context, broadcast_message, target_users_list): blocked_users_list, unreachable_users_list, unexpected_error_occurred_users_list = [], [], [] for user_id in target_users_list: try: if user_id == EMPTY_USER_ID: continue context.bot.send_message(chat_id=user_id, text=broadcast_message) except telegram.error.Unauthorized: blocked_users_list.append(user_id) except telegram.error.BadRequest: unreachable_users_list.append(user_id) except Exception as e: error_message = MessageTemplates.gen_error_message_while_sending_to_user( user_id) LoggerManager.exception_logs(e) LoggerManager.exception_logs(error_message) unexpected_error_occurred_users_list.append(user_id) error_occurred_while_sending_to_users_dict = SharedFunctions.gen_broadcast_error_for_users_report( blocked_users_list, unreachable_users_list, unexpected_error_occurred_users_list) return error_occurred_while_sending_to_users_dict
def add_voucher_to_user_wallet(user_selected_voucher, user_id, username): expiry_date = TimeManager.gen_expiry_date() voucher_id_in_db = user_selected_voucher[1] voucher_title = user_selected_voucher[2] exchange_fee = user_selected_voucher[9] voucher_to_be_added_into_db = { 'telegram_id': user_id, 'asset_type': 'coupon', 'asset_id': voucher_id_in_db, 'asset_value': 1, 'expiry_date': expiry_date } result = UserManager.add_user_voucher(voucher_to_be_added_into_db) if result: exchange_voucher_id_to_be_updated = user_selected_voucher[8] VoucherManager.update_voucher_details( exchange_voucher_id_to_be_updated, 'exchangecoupons', 'issueamount') UserManager.update_user_snapcoin_amt(exchange_fee, '-', user_id) UserManager.update_user_voucher_history(user_id, 'exchanged', voucher_id_in_db) return True LoggerManager.exception_logs( f"Error occurred while adding {voucher_title} voucher to user {username} ({user_id})'s wallet!" )
def get_list_of_admins(): try: with open(_admin_info_file_path, 'r') as file: return [line.strip() for line in file] except Exception as e: LoggerManager.exception_logs(e) LoggerManager.exception_logs("Error occurred while getting admin list")
def get_all_allocated_new_user_vouchers(): query_statement = ''' select title, teleimageurl, couponid, businesshours, location, website, dailycoupon_table.id, phone from snapshop_property.coupons coupon_table inner join snapshop_property.dailycoupons dailycoupon_table on coupon_table.id = dailycoupon_table.couponid inner join snapshop_property.shops shop_table on coupon_table.shopid = shop_table.id where dailycoupon_table.issueamount != dailycoupon_table.totalamount and dailycoupon_table.action = 'new_user' and coupon_table.id >= 13 and isdeleted = 0 ''' try: return db_manager.query_database_no_conditions(query_statement) except Exception as e: LoggerManager.exception_logs(e)
def get_weekly_allocated_voucher(voucher_id): query_statement = ''' select title, teleimageurl, couponid, businesshours, location, website, dailycoupon_table.id, phone from snapshop_property.coupons coupon_table inner join snapshop_property.dailycoupons dailycoupon_table on coupon_table.id = dailycoupon_table.couponid inner join snapshop_property.shops shop_table on coupon_table.shopid = shop_table.id where dailycoupon_table.issueamount != dailycoupon_table.totalamount and dailycoupon_table.id = %s and action = 'weekly_login' and isdeleted = 0 ''' value = (voucher_id, ) try: return db_manager.query_database_with_conditions( query_statement, value) except Exception as e: LoggerManager.exception_logs(e)
def is_valid_choice(user_choice_str_format, context, user_id): try: user_choice = int(user_choice_str_format) - 1 user_selected_voucher = "" if user_choice >= MINIMUM_VOUCHER_INDEX_RANGE: user_selected_voucher = context.user_data[user_id]['claimable_vouchers'][user_choice] return user_selected_voucher except Exception as e: LoggerManager.exception_logs(e)
def dispatch_voucher(update, voucher_path, caption_to_send): try: LoggerManager.general_logs( f"Sending voucher to {update.message.from_user.username} ({update.message.from_user.id}) now" ) update.message.reply_photo(photo=open(voucher_path, 'rb'), caption=caption_to_send) except Exception as e: update.message.reply_text(MessageTemplates.error_message) LoggerManager.exception_logs(e)
def get_random_new_user_voucher(): vouchers = VoucherManager.get_all_allocated_new_user_vouchers() if len(vouchers) == 0: LoggerManager.exception_logs( "There are not vouchers allocated for new user bonus!") if len(vouchers) > 1: random_voucher_index = randomize_voucher_index(len(vouchers)) else: random_voucher_index = 0 return vouchers[random_voucher_index]
def update_voucher_details(voucher_id, table, field): update_statement = f''' update snapshop_property.{table} set {field} = {field} + 1 where id = %s ''' value = (voucher_id, ) try: return db_manager.update_database_table(update_statement, value) except Exception as e: LoggerManager.exception_logs(e)
def add_coin_to_user_wallet(snapcoin_amount, arithmetic_operator, user_id, username): result = UserManager.update_user_snapcoin_amt(snapcoin_amount, arithmetic_operator, user_id) logging_message = f"{snapcoin_amount} to user {username} ({user_id})'s wallet!" if result: LoggerManager.general_logs(f"Successfully added {logging_message}") return True else: LoggerManager.exception_logs( f"Error occurred while adding {logging_message}")
def get_shop_by_code_entered(code_entered): query_statement = ''' select code from snapshop_property.shops where code = %s ''' value = (code_entered, ) try: return db_manager.query_database_with_conditions( query_statement, value) except Exception as e: LoggerManager.exception_logs(e)
def send_message_to_snapee_assistant(update, context, snapee_asst_id, photo_id): user_id = update.message.from_user.id username = update.message.from_user.username LoggerManager.general_logs( f"Sending user {username} ({user_id})'s photo to snapeeassistant now.") try: context.bot.send_photo(snapee_asst_id, photo=photo_id, caption=f"User: {username} ({user_id})") return True except Exception as e: LoggerManager.exception_logs(e) LoggerManager.exception_logs( f"Something went wrong while sending user {username} ({user_id})'s photo to SnapeeAssistant" )
def validate_referral_code(message_from_user): LoggerManager.general_logs(f"Verifying referral code {message_from_user}") referrer_telegram_id_and_snapcoin_amt = UserManager.is_valid_referral_code( message_from_user) if not referrer_telegram_id_and_snapcoin_amt: LoggerManager.exception_logs( f"Referral code: {message_from_user} is invalid! Prompting user to re-enter again.") return referrer_telegram_id = referrer_telegram_id_and_snapcoin_amt[0][0] referrer_snapcoin_amt = referrer_telegram_id_and_snapcoin_amt[0][1] LoggerManager.general_logs( f"Referral code found! Referrer's telegram id: {referrer_telegram_id} ({referrer_snapcoin_amt}) snapcoins.") return referrer_telegram_id_and_snapcoin_amt
def query_database_no_conditions(query_statement): try: conn = init_db_connection() if conn.is_connected(): cursor = conn.cursor() cursor.execute(query_statement) result = cursor.fetchall() cursor.close() conn.close() return result else: LoggerManager.general_logs( 'Connection to SQL DB cannot be established.') conn.close() except Error as e: LoggerManager.exception_logs(e)
def update_database_table(update_statement, values): try: conn = init_db_connection() if conn.is_connected(): cursor = conn.cursor() cursor.execute(update_statement, values) conn.commit() cursor.close() conn.close() return True else: LoggerManager.general_logs( 'Connection to SQL DB cannot be established.') conn.close() except Error as e: LoggerManager.exception_logs(e)
def add_vouchers_to_user_wallet(code_entered_by_user, vouchers_in_promo_code, update): user_id = update.message.from_user.id username = update.message.from_user.username LoggerManager.general_logs( f"Inserting promo vouchers into user {username} ({user_id})'s wallet in db" ) for voucher in vouchers_in_promo_code: voucher_id_in_db = voucher[1] voucher_title = voucher[2] expiry_date = voucher[8] voucher_to_be_added_into_db = { 'telegram_id': user_id, 'asset_type': 'coupon', 'asset_id': voucher_id_in_db, 'asset_value': 1, 'expiry_date': expiry_date } result = UserManager.add_user_voucher(voucher_to_be_added_into_db) if not result: LoggerManager.exception_logs( f"Error occurred while adding {voucher_title} into the db for user {username} ({user_id})" ) return promo_voucher_id = vouchers_in_promo_code[0][0] VoucherManager.update_voucher_details(promo_voucher_id, 'promocode', 'claimed_amount') UserManager.update_user_voucher_history(user_id, code_entered_by_user, None) return True
def send_registration_result(update, context, registration_result): user_id = update.message.from_user.id username = update.message.from_user.username if not registration_result: LoggerManager.exception_logs( f"Error occurred while registering user {username} ({user_id})!") message_to_send = MessageTemplates.error_message update.message.reply_text(message_to_send) return LoggerManager.general_logs( f"User {username} ({user_id}) registered successfully!") message_to_send = MessageTemplates.gen_welcome_new_user_message( username) update.message.reply_text(message_to_send) send_new_user_bonus_notification(context, user_id)
def get_all_promo_code_vouchers(code_entered_by_user, date_today): query_statement = ''' select promo_code_table.id, coupon_table.id as coupon_id, title, teleimageurl, businesshours, location, website, phone, expireto from snapshop_property.coupons coupon_table inner join snapshop_property.shops shop_table on coupon_table.shopid = shop_table.id inner join snapshop_property.promocoupon promo_coupon_table on promo_coupon_table.coupon_id = coupon_table.id inner join snapshop_property.promocode promo_code_table on promo_code_table.id = promo_coupon_table.promo_code_id where promo_code_table.code = %s and promo_code_table.claimed_amount < promo_code_table.amount_claimable and promo_code_table.end_date >= %s ''' values = ( code_entered_by_user, date_today, ) try: return db_manager.query_database_with_conditions( query_statement, values) except Exception as e: LoggerManager.exception_logs(e)