def history_text(user_id, page: int) -> str: user = get_user(user_id, 'ins') if isinstance(user, str): raise Exception(user) message_list = [] try: history_response = api.messages.getHistory(user_id=user['id'], offset=page * 20)['items'] except exceptions.VkException as e: raise e for i in history_response: message_body = i['body'] if len(message_body) > 120: message_body = message_body[:120] if i['out'] == 0: message_list.append({'>>> ': message_body}) else: message_list.append({'<<< ': message_body}) text_form = 'Сообщения с <b>' + user['first_name'] + ' ' + user[ 'last_name'] + '</b>(' + str(page) + ')\n' for item in message_list: for k, v in item.items(): text_form = text_form + '\n' + k + utils.escapize(v) user_nom = get_user(user) if isinstance(user_nom, str): raise Exception(user_nom) nom_name = user_nom['first_name'] + ' ' + user_nom['last_name'] utils.dbadd('activity', '📃️ ' + nom_name + ' - ' + str(user_nom['id'])) keen.add_event("history", {"to_user": user_nom['id']}) return text_form
def sethook(bot, update, cmd=None): if not cmd: return blacklist = utils.dbget('hook') if blacklist is not None: if str(update.message.from_user.id) in blacklist: update.message.reply_text( emojize(choice(utils.blacklist_strings), use_aliases=True)) return check_black = utils.dbget('notarget') hook_vk_user = get_user(cmd[0]) if isinstance(hook_vk_user, str): update.message.reply_text( emojize('Ошибка :disappointed:', use_aliases=True)) return reply_text = emojize('Хорошо :ok_hand:', use_aliases=True) if check_black is not None: if str(hook_vk_user['id']) in check_black: reply_text = hook_vk_user['first_name'] + ' ' + hook_vk_user[ 'last_name'] + ' помечен как спамер!\nЕсли этот человек будет спамить вам, вы можете ввести /delhook ' + str( hook_vk_user['id'] ) + ' чтобы перестать получать от него сообшения' utils.dbadd(hook_vk_user['id'], update.message.chat.id) update.message.reply_text(reply_text) keen.add_event("set_hook", { "by_user": update.message.from_user.id, "req_user": cmd[0] })
def poll_callback(target, user_id, text, attachments): u = get_user(user_id) full_name = utils.escapize(u['first_name'] + ' ' + u['last_name']) utils.dbadd('activity', '✉️ ' + full_name + ' - ' + str(u['id'])) if not attachments: try: tg.sendMessage(chat_id=target, text='<b>' + u['first_name'] + ' ' + u['last_name'] + ' >>></b>\n' + utils.escapize(text), parse_mode='HTML', disable_web_page_preview=True) except Exception as e: tg.send_message( admin, 'Exception:\n' + str(e) + '\nIn poll_callback while sending to target - ' + str(target)) for a in attachments: if a['type'] == 'photo': try: with open(a['pic'], 'rb') as p: tg.send_photo(chat_id=target, caption=u['first_name'] + ' ' + u['last_name'] + ' >>> ' + text, photo=p) except Exception as e: tg.send_message( admin, 'Exception:\n' + str(e) + '\nIn poll_callback while sending to target - ' + str(target) + '\nUser_id - ' + str(user_id)) elif a['type'] == 'doc': pass
def check_unread(): dialogs = api.messages.getDialogs(count=200, unread=True) if dialogs['count'] > 0: msg_ids = [] d = utils.plural(dialogs['count']) response = '<b>' + str( dialogs['count'] ) + ' ' + d + ' с непрочитанными сообщениями:</b>\n' for i in dialogs['items']: sleep(2) user_id = i['message']['user_id'] msg_ids.append(i['message']['id']) try: user = api.users.get(user_ids=user_id)[0] full_name = utils.escapize(user['first_name'] + ' ' + user['last_name']) response += full_name + ' ' + str(user_id) + '\n' except exceptions.VkException: pass # User notification if user_id > 0: targets = utils.dbget(user_id) if targets is not None: for t in targets: u = api.users.get(user_ids=user_id, name_case='gen')[0] u_nom = api.users.get(user_ids=user_id)[0] full_name = utils.escapize(u['first_name'] + ' ' + u['last_name']) full_name_nom = utils.escapize(u_nom['first_name'] + ' ' + u_nom['last_name']) sud = str(u_nom['id']) utils.dbadd('activity', '✉️ ' + full_name_nom + ' - ' + sud) text = 'Есть новые сообщения от <b>' + full_name + '.</b> Id: ' + sud + '\nВведите <code>/d ' + sud + '</code> чтобы получить историю сообщений' try: tg.sendMessage(chat_id=t, text=text, parse_mode='HTML', disable_web_page_preview=True) except Exception as e: et = 'Exception:\n' + str( e ) + '\nIn check_unread while sending to target - ' + str( t) # noinspection PyTypeChecker tg.send_message(admin, et) sleep(2) tg.send_message(log_channel, response, 'HTML', True) try: api.messages.markAsRead(message_ids=msg_ids) except exceptions.VkException as e: ete = 'Exception:\n' + str( e) + '\nIn check_unread while markAsRead' # noinspection PyTypeChecker tg.send_message(admin, ete)
def send(bot, update, cmd=None): blacklist = utils.dbget('send') if blacklist is not None: if str(update.message.from_user.id) in blacklist: update.message.reply_text( emojize(choice(utils.blacklist_strings), use_aliases=True)) return if not cmd: return user = get_user(cmd[0]) vkb = utils.dbget('vkblacklist') if vkb is not None: if str(user['id']) in vkb: update.message.reply_text( 'Вы не можете отправлять сообщения этому пользователю') tg.send_message( admin, str(update.message.from_user.id) + '\n' + str(update.message.text)) return data = '' try: data = api.messages.send(peer_id=user['id'], message=cmd[1]) except exceptions.VkException as exception: data = str(exception) except TypeError: data = emojize('Что-то пошло не так :confused:', use_aliases=True) # if not enough args finally: if isinstance(data, int): full_name = utils.escapize(user['first_name'] + ' ' + user['last_name']) t = '<b>' + full_name + ' <<<</b>\n' + utils.escapize( cmd[1]) update.message.reply_text(t, parse_mode='HTML', disable_web_page_preview=True) tg.send_message(log_channel, t, 'HTML', True) utils.dbadd('activity', '✉️ ' + full_name + ' - ' + str(user['id'])) keen.add_event("sended", { "by_user": update.message.from_user.id, "to_user": user['id'] }) else: update.message.reply_text(str(data))
def like(bot, update, cmd=None): print('Call like(): ' + str(update.message.text)) blacklist = utils.dbget('like') if blacklist is not None: if str(update.message.from_user.id) in blacklist: update.message.reply_text( emojize(choice(utils.blacklist_strings), use_aliases=True)) return if not cmd: return try: user = api.users.get(user_ids=cmd[0])[0] owner = user['id'] name = user['first_name'] + ' ' + user['last_name'] except exceptions.VkException: group = api.groups.getById(group_id=cmd[0])[0] owner = 0 - group['id'] name = group['name'] print('calling db') available = utils.db_like(update.message.from_user.id) print(str(available) + ' likes for ' + str(update.message.from_user.id)) count = 10 if available >= 10 else available if count <= 0: update.message.reply_text( emojize('У вас не осталось сердечек :disappointed:', use_aliases=True)) keen.add_event("not_enough_user_likes", {"by_user": update.message.from_user.id}) return elif utils.limits() <= count: update.message.reply_text( emojize('Исчерапан дневной лимит бота :disappointed:', use_aliases=True)) keen.add_event("not_enough_global_likes", {"by_user": update.message.from_user.id}) return else: q.put([ owner, update.message.chat.id, name, count, update.message.from_user.id, update.message.message_id ]) update.message.reply_text( emojize('Ок, выполняю :sparkling_heart:', use_aliases=True)) utils.dbadd('activity', '❤️️ ' + name + ' - ' + str(owner))
def blacklist_control(bot, update, cmd=None): if not cmd: return try: if cmd[0] == 'add': for i in cmd[1].split(): utils.dbadd(i, str(update.message.reply_to_message.from_user.id)) tg.send_message(admin, str(utils.dbget(i))) sleep(2) if cmd[0] == 'del': for i in cmd[1].split(): utils.dbdel(i, str(update.message.reply_to_message.from_user.id)) tg.send_message(admin, str(utils.dbget(i))) sleep(2) update.message.reply_text('Успешно') except Exception as e: tg.send_message(admin, str(e)) update.message.reply_text('Nope.')
def friend(bot, update, cmd=None): if cmd: try: user = api.users.get(user_ids=cmd[0])[0] except exceptions.VkException as exception: update.message.reply_text(str(exception)) return try: api.friends.add(user_id=user['id']) except exceptions.VkException as e: update.message.reply_text(str(e)) return update.message.reply_text(emojize('Хорошо :relaxed:', use_aliases=True)) name = user['first_name'] + ' ' + user['last_name'] utils.dbadd('activity', '➕️️ ' + name + ' - ' + str(user['id'])) keen.add_event("friend", { "by_user": update.message.from_user.id, "req_user": cmd[0] })
def fromvkblack(bot, update, cmd=None): if not cmd: return utils.dbadd('notarget', str(get_user(cmd[0])['id'])) tg.send_message(admin, str(utils.dbget('notarget')))