def send_unread_notifications(msg_vk_api): users_notifications = db_api.Notification().get_new_items() for nt_info in users_notifications: message = notification_constructor(nt_info) actions = vk_api.get_actions_from_buttons(message['keyboard']) message['message'] = 'Вы получили новое уведомление!\n\n' + message[ 'message'] if msg_vk_api.send_notification(nt_info.owner_id, message): db_api.AvailableActions().update_actions(nt_info.owner_id, actions) nt_info.is_read = True nt_info.save() db_api.User().default_settings(nt_info.owner_id) user_info = db_api.User().get_user(nt_info.owner_id) user_info.item_id = nt_info.id user_info.save() logging.info(f'send notification {nt_info.owner_id}')
def main(user): db_api.User().default_settings(user.info.id) message = 'Главное меню' button_profiles = vk_api.new_button('Мои анкеты', {'m_id': 'user_profiles'}) button_search = vk_api.new_button('Найти соигроков', {'m_id': 'profiles_search'}) admin_button = [] if user.info.is_admin: admin_button = [ vk_api.new_button('Меню админа', {'m_id': 'admin_menu'}) ] notifications_list = db_api.Notification().get_item_list(user.info.id) notification_count = len([i for i in notifications_list if not i.is_read]) notifications_label = 'Мои Уведомления' + (f' ({notification_count})' if notification_count else '') notifications_color = 'positive' if notification_count else 'default' button_notifications = vk_api.new_button(notifications_label, {'m_id': 'notifications'}, notifications_color) return { 'message': message, 'keyboard': [[button_profiles], [button_search], [button_notifications], admin_button] }
def update_images(message): print('update_images', message) if not message: return False images_attachments = [ i['photo'] for i in message['attachments'] if i['type'] == 'photo' ] images = [ f"photo{i['owner_id']}_{i['id']}_{i.get('access_key', '')}" for i in images_attachments ] print('images', images) user_info = db_api.User().get_user(message['peer_id']) if user_info: rp_profile = db_api.RpProfile().get_profile(user_info.item_id) if rp_profile: rp_profile.arts = json.dumps(images, ensure_ascii=False) rp_profile.save() return True return False
while 1: try: new_messages = long_poll_stdout.get(timeout=120) except queue.Empty: long_poll_stdout, long_poll_listner = init_messages_get_thread() continue timer.start('total') for msg in new_messages: print('usr msg:', msg) user_token = Token(msg) if not user_token.info: vk_user_info = bot_api.get_user_info(msg['from_id']) try: user_info = db_api.User().create_user(user_id=vk_user_info['id'], name=vk_user_info['first_name'], is_fem=vk_user_info['sex'] % 2) user_token.update_user_info(user_info, msg) except: continue timer.start(user_token.menu_id) bot_message = menu.menu_hub(user_token) timer.time_stamp(user_token.menu_id) actions = vk_api.get_actions_from_buttons(bot_message['keyboard']) db_api.AvailableActions().update_actions(msg['from_id'], actions) if user_token.menu_id == 'save_images': msg_id = bot_api.msg_send(msg['peer_id'], bot_message) if msg_id:
def __init__(self, message): self.msg_text = message['text'] self.msg_attach = message['attachments'] user = db_api.User().get_user(message['from_id']) if user: self.update_user_info(user, message)