def list_squad_requests(bot: Bot, update: Update): session = Session() admin = session.query(Admin).filter_by( user_id=update.message.from_user.id).all() group_admin = [] for adm in admin: if adm.admin_type == AdminType.GROUP.value and adm.admin_group != 0: group_admin.append(adm) count = 0 for adm in group_admin: members = session.query(SquadMember).filter_by( squad_id=adm.admin_group, approved=False) for member in members: count += 1 markup = generate_squad_request_answer(member.user_id) send_async(bot, chat_id=update.message.chat.id, text=fill_char_template(MSG_PROFILE_SHOW_FORMAT, member.user, member.user.character, True), reply_markup=markup) if count == 0: send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_REQUEST_EMPTY)
def generate_group_info(group_id): session = Session() group = session.query(Group).filter(Group.id == group_id).first() admins = session.query(Admin).filter(Admin.admin_group == group_id).all() adm_msg = '' adm_del_keys = [] for adm in admins: user = session.query(User).filter_by(id=adm.user_id).first() adm_msg += MSG_GROUP_STATUS_ADMIN_FORMAT.format( user.id, user.username or '', user.first_name or '', user.last_name or '') adm_del_keys.append([ InlineKeyboardButton(MSG_GROUP_STATUS_DEL_ADMIN.format( user.first_name or '', user.last_name or ''), callback_data=json.dumps({ 't': QueryType.DelAdm.value, 'uid': user.id, 'gid': group_id })) ]) msg = MSG_GROUP_STATUS.format( group.title, adm_msg, MSG_ON if group.welcome_enabled else MSG_OFF, MSG_ON if group.allow_trigger_all else MSG_OFF, MSG_ON if len(group.squad) and group.squad[0].thorns_enabled else MSG_OFF) adm_del_keys.append([ InlineKeyboardButton(MSG_BACK, callback_data=json.dumps( {'t': QueryType.GroupList.value})) ]) inline_markup = InlineKeyboardMarkup(adm_del_keys) return msg, inline_markup
def generate_group_manage(group_id): session = Session() squads = session.query(Squad).all() inline_keys = [] for squad in squads: in_group = False for item in squad.chat.group_items: if item.group_id == group_id: in_group = True break inline_keys.append([ InlineKeyboardButton( (MSG_SYMBOL_ON if in_group else MSG_SYMBOL_OFF) + squad.squad_name, callback_data=json.dumps({ 't': QueryType.OrderGroupTriggerChat.value, 'id': group_id, 'c': squad.chat_id })) ]) inline_keys.append([ InlineKeyboardButton(MSG_ORDER_GROUP_DEL, callback_data=json.dumps({ 't': QueryType.OrderGroupDelete.value, 'id': group_id })) ]) inline_keys.append([ InlineKeyboardButton(MSG_BACK, callback_data=json.dumps( {'t': QueryType.OrderGroupList.value})) ]) return InlineKeyboardMarkup(inline_keys)
def generate_order_chats_markup(bot: Bot, pin=True): session = Session() squads = session.query(Squad).all() inline_keys = [] for squad in squads: inline_keys.append([ InlineKeyboardButton(squad.squad_name, callback_data=json.dumps({ 't': QueryType.Order.value, 'g': False, 'id': squad.chat_id })) ]) inline_keys.append([ InlineKeyboardButton(MSG_ORDER_PIN if pin else MSG_ORDER_NO_PIN, callback_data=json.dumps({ 't': QueryType.TriggerOrderPin.value, 'g': False })) ]) inline_markup = InlineKeyboardMarkup(inline_keys) return inline_markup
def set_admin(bot: Bot, update: Update): try: msg = update.message.text.split(' ', 1)[1] msg = msg.replace('@', '') if msg != '': session = Session() user = session.query(User).filter_by(username=msg).first() if user is None: send_async(bot, chat_id=update.message.chat.id, text=MSG_USER_UNKNOWN) else: adm = session.query(Admin).filter_by( user_id=user.id, admin_group=update.message.chat.id).first() if adm is None: new_group_admin = Admin(user_id=user.id, admin_type=AdminType.GROUP.value, admin_group=update.message.chat.id) session.add(new_group_admin) session.commit() send_async(bot, chat_id=update.message.chat.id, text=MSG_NEW_GROUP_ADMIN.format(user.username)) else: send_async(bot, chat_id=update.message.chat.id, text=MSG_NEW_GROUP_ADMIN_EXISTS.format( user.username)) except Exception as e: Session.rollback()
def order_status(order_id): try: session = Session() order = session.query(Order).filter_by(id=order_id).first() if order is not None: users = [] for order_ok in order.cleared: users.append({ 'username': order_ok.user.username, 'id': order_ok.user.id, 'attack': order_ok.user.character.attack if order_ok.user.character else 0, 'defence': order_ok.user.character.defence if order_ok.user.character else 0 }) return flask.Response(status=200, mimetype="application/json", response=json.dumps({'users': users})) except: Session.rollback() return flask.Response(status=400)
def ready_to_battle(bot: Bot, job_queue): session = Session() try: group = session.query(Squad).all() for item in group: new_order = Order() new_order.text = MSG_MAIN_READY_TO_BATTLE new_order.chat_id = item.chat_id new_order.date = datetime.now() new_order.confirmed_msg = 0 session.add(new_order) session.commit() callback_data = json.dumps( {'t': QueryType.OrderOk.value, 'id': new_order.id}) markup = InlineKeyboardMarkup([ [InlineKeyboardButton(MSG_MAIN_INLINE_BATTLE, callback_data=callback_data)]]) msg = send_order(bot, new_order.text, 0, new_order.chat_id, markup) try: msg = msg.result().result() if msg is not None: bot.request.post(bot.base_url + '/pinChatMessage', {'chat_id': new_order.chat_id, 'message_id': msg.message_id, 'disable_notification': False}) except TelegramError as err: bot.logger.error(err.message) except SQLAlchemyError as err: bot.logger.error(str(err)) Session.rollback()
def list_triggers(bot: Bot, update: Update): session = Session() triggers = session.query(Trigger).all() local_triggers = session.query(LocalTrigger).filter_by( chat_id=update.message.chat.id).all() msg = MSG_TRIGGER_LIST_HEADER + \ '<b>Глобальные:</b>\n' + ('\n'.join([trigger.trigger for trigger in triggers]) or MSG_EMPTY) + \ '\n<b>Локальные:</b>\n' + ('\n'.join([trigger.trigger for trigger in local_triggers]) or MSG_EMPTY) send_async(bot, chat_id=update.message.chat.id, text=msg, parse_mode=ParseMode.HTML)
def del_squad(bot: Bot, update: Update): session = Session() squad = session.query(Squad).filter_by( chat_id=update.message.chat.id).first() if update.message.chat.type == 'supergroup' and squad is not None: for member in squad.members: session.delete(member) session.delete(squad) session.commit() send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_DELETE)
def newbie(bot: Bot, update: Update): if update.message.chat.id in [-1001045426965]: if update.message.new_chat_member is not None: session = Session() user = session.query(User).filter( User.id == update.message.new_chat_member.id).first() if user is None: group = session.query(Group).filter( Group.id == -1001146975451).first() if group is not None: send_async(bot, chat_id=group.id, text=fill_template( MSG_NEWBIE, update.message.new_chat_member))
def generate_squad_request(): session = Session() inline_keys = [] squads = session.query(Squad).filter_by(hiring=True).all() for squad in squads: inline_keys.append([ InlineKeyboardButton(squad.squad_name, callback_data=json.dumps({ 't': QueryType.RequestSquad.value, 'id': squad.chat_id })) ]) return InlineKeyboardMarkup(inline_keys)
def admin_panel(bot: Bot, update: Update): if update.message.chat.type == 'private': session = Session() admin = session.query(Admin).filter_by( user_id=update.message.from_user.id).all() full_adm = False grp_adm = False for adm in admin: if adm.admin_type <= AdminType.FULL.value: full_adm = True else: grp_adm = True send_async(bot, chat_id=update.message.chat.id, text=MSG_ADMIN_WELCOME, reply_markup=generate_admin_markup(full_adm, grp_adm))
def send_status(bot: Bot, update: Update): session = Session() msg = MSG_GROUP_STATUS_CHOOSE_CHAT squads = session.query(Squad).all() inline_keys = [] for squad in squads: inline_keys.append( InlineKeyboardButton(squad.squad_name, callback_data=json.dumps({ 't': QueryType.GroupInfo.value, 'id': squad.chat_id }))) inline_markup = InlineKeyboardMarkup([[key] for key in inline_keys]) send_async(bot, chat_id=update.message.chat.id, text=msg, reply_markup=inline_markup)
def help_msg(bot: Bot, update): session = Session() admin_user = session.query(Admin).filter_by( user_id=update.message.from_user.id).all() global_adm = False for adm in admin_user: if adm.admin_type <= AdminType.FULL.value: global_adm = True break if global_adm: send_async(bot, chat_id=update.message.chat.id, text=MSG_HELP_GLOBAL_ADMIN) elif len(admin_user) != 0: send_async(bot, chat_id=update.message.chat.id, text=MSG_HELP_GROUP_ADMIN) else: send_async(bot, chat_id=update.message.chat.id, text=MSG_HELP_USER)
def trigger_show(bot: Bot, update: Update): session = Session() trigger = session.query(LocalTrigger).filter_by( chat_id=update.message.chat.id, trigger=update.message.text).first() if trigger is None: trigger = session.query(Trigger).filter_by( trigger=update.message.text).first() if trigger is not None: if trigger.message_type == MessageType.AUDIO.value: bot.send_audio(update.message.chat.id, trigger.message) elif trigger.message_type == MessageType.DOCUMENT.value: bot.send_document(update.message.chat.id, trigger.message) elif trigger.message_type == MessageType.VOICE.value: bot.send_voice(update.message.chat.id, trigger.message) elif trigger.message_type == MessageType.STICKER.value: bot.send_sticker(update.message.chat.id, trigger.message) elif trigger.message_type == MessageType.CONTACT.value: msg = trigger.message.replace('\'', '"') contact = loads(msg) if 'phone_number' not in contact.keys(): contact['phone_number'] = None if 'first_name' not in contact.keys(): contact['first_name'] = None if 'last_name' not in contact.keys(): contact['last_name'] = None bot.send_contact(update.message.chat.id, contact['phone_number'], contact['first_name'], contact['last_name']) elif trigger.message_type == MessageType.VIDEO.value: bot.send_video(update.message.chat.id, trigger.message) elif trigger.message_type == MessageType.VIDEO_NOTE.value: bot.send_video_note(update.message.chat.id, trigger.message) elif trigger.message_type == MessageType.LOCATION.value: msg = trigger.message.replace('\'', '"') location = loads(msg) bot.send_location(update.message.chat.id, location['latitude'], location['longitude']) elif trigger.message_type == MessageType.PHOTO.value: bot.send_photo(update.message.chat.id, trigger.message) else: send_async(bot, chat_id=update.message.chat.id, text=trigger.message, disable_web_page_preview=True)
def close_hiring(bot: Bot, update: Update): session = Session() squad = session.query(Squad).filter_by( chat_id=update.message.chat.id).first() if squad is not None: squad.hiring = False session.add(squad) session.commit() send_async(bot, chat_id=update.message.chat.id, text='Набор закрыт')
def generate_groups_manage(): session = Session() groups = session.query(OrderGroup).all() inline_keys = [] for group in groups: inline_keys.append([ InlineKeyboardButton(group.name, callback_data=json.dumps({ 't': QueryType.OrderGroupManage.value, 'id': group.id })) ]) inline_keys.append([ InlineKeyboardButton(MSG_ORDER_GROUP_ADD, callback_data=json.dumps( {'t': QueryType.OrderGroupAdd.value})) ]) return InlineKeyboardMarkup(inline_keys)
def order(bot: Bot, update: Update, chat_data): session = Session() admin_user = session.query(Admin).filter( Admin.user_id == update.message.from_user.id).all() markup = generate_order_groups_markup( bot, admin_user, chat_data['pin'] if 'pin' in chat_data else True) msg = update.message if msg.audio: chat_data['order'] = msg.audio.file_id chat_data['order_type'] = MessageType.AUDIO.value elif msg.document: chat_data['order'] = msg.document.file_id chat_data['order_type'] = MessageType.DOCUMENT.value elif msg.voice: chat_data['order'] = msg.voice.file_id chat_data['order_type'] = MessageType.VOICE.value elif msg.sticker: chat_data['order'] = msg.sticker.file_id chat_data['order_type'] = MessageType.STICKER.value elif msg.contact: chat_data['order'] = str(msg.contact) chat_data['order_type'] = MessageType.CONTACT.value elif msg.video: chat_data['order'] = msg.video.file_id chat_data['order_type'] = MessageType.VIDEO.value elif msg.video_note: chat_data['order'] = msg.video_note.file_id chat_data['order_type'] = MessageType.VIDEO_NOTE.value elif msg.location: chat_data['order'] = str(msg.location) chat_data['order_type'] = MessageType.LOCATION.value elif msg.photo: chat_data['order'] = msg.photo[-1].file_id chat_data['order_type'] = MessageType.PHOTO.value else: chat_data['order'] = msg.text chat_data['order_type'] = MessageType.TEXT.value send_async(bot, chat_id=update.message.chat.id, text=MSG_ORDER_SEND_HEADER, reply_markup=markup)
def check_bot_in_chats(bot: Bot, update: Update): session = Session() groups = session.query(Group).filter_by(bot_in_group=True).all() for group in groups: try: bot.getChatMember(group.id, bot.id) except TelegramError as e: group.bot_in_group = False session.add(group) session.commit()
def add_global_trigger(bot: Bot, update: Update): msg = update.message.text.split(' ', 1) if len(msg) == 2 and len(msg[1]) > 0 and update.message.reply_to_message: trigger_text = msg[1].strip() session = Session() trigger = session.query(Trigger).filter_by( trigger=trigger_text).first() if trigger is None: data = update.message.reply_to_message add_global_trigger_db(data, trigger_text) send_async(bot, chat_id=update.message.chat.id, text=MSG_TRIGGER_NEW.format(trigger_text)) else: send_async(bot, chat_id=update.message.chat.id, text=MSG_TRIGGER_EXISTS.format(trigger_text)) else: send_async(bot, chat_id=update.message.chat.id, text=MSG_TRIGGER_NEW_ERROR)
def del_adm(bot, chat_id, user): session = Session() try: adm = session.query(Admin).filter_by(user_id=user.id, admin_group=chat_id).first() if adm is None: send_async(bot, chat_id=chat_id, text=MSG_DEL_GROUP_ADMIN_NOT_EXIST.format( user.username)) else: session.delete(adm) session.commit() send_async(bot, chat_id=chat_id, text=MSG_DEL_GROUP_ADMIN.format(user.username)) except: session.rollback()
def new_ready_to_battle(chat_id): session = Session() try: order = Order() order.chat_id = chat_id order.confirmed_msg = 0 order.text = 'К битве готовсь!' order.date = datetime.now() session.add(order) session.commit() order = session.query(Order).filter_by( chat_id=chat_id, date=order.date, text='К битве готовсь!').first() return flask.Response(status=200, mimetype="application/json", response=json.dumps({'order_id': order.id})) except: Session.rollback() return flask.Response(status=400)
def squad_request(bot: Bot, update: Update): session = Session() user = session.query(User).filter_by( id=update.message.from_user.id).first() if user is not None: if user.character: if user.member: markup = generate_leave_squad(user.id) send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_REQUEST_EXISTS, reply_markup=markup) else: markup = generate_squad_request() send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_REQUEST, reply_markup=markup) else: send_async(bot, chat_id=update.message.chat.id, text='Сначала дай мне профиль!')
def disable_thorns(bot: Bot, update: Update): session = Session() group = session.query(Group).filter_by(id=update.message.chat.id).first() if update.message.chat.type == 'supergroup' and group is not None and len( group.squad) == 1: group.squad[0].thorns_enabled = False session.add(group.squad[0]) session.commit() send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_THORNS_DISABLED)
def show_welcome(bot: Bot, update): if update.message.chat.type in ['group', 'supergroup']: session = Session() group = update_group(update.message.chat) welcome_msg = session.query(WelcomeMsg).filter_by( chat_id=group.id).first() if welcome_msg is None: welcome_msg = WelcomeMsg(chat_id=group.id, message=MSG_WELCOME_DEFAULT) session.add(welcome_msg) session.commit() send_async(bot, chat_id=group.id, text=welcome_msg.message)
def add_to_squad(bot: Bot, update: Update): session = Session() squad = session.query(Squad).filter_by( chat_id=update.message.chat.id).first() if squad is not None: username = update.message.text.split(' ', 1) if len(username) == 2: username = username[1].replace('@', '') user = session.query(User).filter_by(username=username).first() if user is not None and user.character is not None and user.member is None: markup = generate_squad_invite_answer(user.id) send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_ADD.format('@' + username), reply_markup=markup) elif user.member is not None: send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_ADD_IN_SQUAD.format('@' + username)) elif user.character is None: send_async(bot, chat_id=update.message.chat.id, text='Сначала пусть даст профиль!')
def generate_squad_list_key(squad): session = Session() attack = 0 defence = 0 members = session.query(SquadMember).filter_by( squad_id=squad.chat_id).all() for member in members: character = session.query(Character).filter_by( user_id=member.user_id).order_by( Character.date.desc()).limit(1).first() attack += character.attack defence += character.defence return [ InlineKeyboardButton('{} : {}⚔ {}🛡 {}👥'.format(squad.squad_name, attack, defence, len(members)), callback_data=json.dumps({ 't': QueryType.MemberList.value, 'id': squad.chat_id })) ]
def disable_trigger_all(bot: Bot, update: Update): session = Session() group = update_group(update.message.chat) group.allow_trigger_all = False session.add(group) session.commit() send_async(bot, chat_id=update.message.chat.id, text=MSG_TRIGGER_ALL_DISABLED)
def send_async(bot: Bot, *args, **kwargs): try: return bot.sendMessage(*args, **kwargs) except TelegramError as err: bot.logger.error(err.message) session = Session() group = session.query(Group).filter_by(id=kwargs['chat_id']).first() if group is not None: group.bot_in_group = False session.add(group) session.commit() return None
def set_squad_name(bot: Bot, update: Update): session = Session() squad = session.query(Squad).filter_by( chat_id=update.message.chat.id).first() if update.message.chat.type == 'supergroup' and squad is not None: msg = update.message.text.split(' ', 1) if len(msg) == 2: squad.squad_name = msg[1] session.add(squad) session.commit() send_async(bot, chat_id=update.message.chat.id, text=MSG_SQUAD_RENAMED.format(squad.squad_name))