Dialogs search

This commit is contained in:
Kylmakalle 2017-07-02 18:32:58 +03:00
parent 907ff31ec0
commit bc95507d8c
2 changed files with 51 additions and 12 deletions

59
bot.py
View File

@ -35,28 +35,22 @@ bot = telebot.AsyncTeleBot(token)
link = 'https://oauth.vk.com/authorize?client_id={}&' \
'display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=friends,messages,offline,docs,photos,video' \
'&response_type=token&v=5.65'.format(vk_app_id)
mark = types.InlineKeyboardMarkup()
yes = types.InlineKeyboardButton('ВХОД', url=link)
mark.add(yes)
def get_pages_switcher(markup, page, pages):
if page != 0:
leftbutton = types.InlineKeyboardButton('', callback_data='page{}'.format(page - 1)) # callback
else:
leftbutton = None
leftbutton = types.InlineKeyboardButton('Поиск 🔍', callback_data='search')
if page + 1 < len(pages):
rightbutton = types.InlineKeyboardButton('', callback_data='page{}'.format(page + 1))
else:
rightbutton = None
if leftbutton and rightbutton:
if rightbutton:
markup.row(leftbutton, rightbutton)
return
if leftbutton:
markup.row(leftbutton)
else:
markup.row(rightbutton)
markup.row(leftbutton)
def replace_shields(text):
@ -117,12 +111,41 @@ def create_markup(message, user, page, edit=False):
parse_mode='HTML', reply_markup=markup).wait()
def search_users(message, text):
session = VkMessage(vk_tokens.get(str(message.from_user.id))).session
markup = types.InlineKeyboardMarkup(row_width=1)
result = vk.API(session).messages.searchDialogs(q=text, limit=10, fields=[])
for chat in result:
if chat['type'] == 'profile':
markup.add(types.InlineKeyboardButton('{} {}'.format(chat['first_name'], chat['last_name']),
callback_data=str(chat['uid'])))
elif chat['type'] == 'chat':
if chat['title'].replace('\\', ''):
chat['title'] = chat['title'].replace('\\', '')
markup.add(
types.InlineKeyboardButton(replace_shields(chat['title']),
callback_data='group' + str(chat['chat_id'])))
if markup.keyboard:
markup.add(types.InlineKeyboardButton('Поиск 🔍', callback_data='search'))
bot.send_message(message.from_user.id, '<b>Результат поиска по</b> <i>{}</i>'.format(text),
reply_markup=markup, parse_mode='HTML')
else:
markup.add(types.InlineKeyboardButton('Поиск 🔍', callback_data='search'))
bot.send_message(message.from_user.id, '<b>Ничего не найдено по запросу</b> <i>{}</i>'.format(text),
parse_mode='HTML', reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def callback_buttons(call):
if call.message:
if 'page' in call.data:
bot.answer_callback_query(call.id).wait()
create_markup(call.message, call.from_user.id, int(call.data.split('page')[1]), True)
elif 'search' in call.data:
markup = types.ForceReply(selective=False)
bot.answer_callback_query(call.id, 'Поиск беседы 🔍').wait()
bot.send_message(call.from_user.id, '<b>Поиск беседы</b> 🔍',
parse_mode='HTML', reply_markup=markup).wait()
elif 'group' in call.data:
session = VkMessage(vk_tokens.get(str(call.from_user.id))).session
chat = vk.API(session).messages.getChat(chat_id=call.data.split('group')[1], fields=[])
@ -134,7 +157,6 @@ def callback_buttons(call):
'<i>Вы в беседе {}</i>'.format(chat['title']),
parse_mode='HTML').wait()
currentchat[str(call.from_user.id)] = call.data
elif call.data.isdigit():
session = VkMessage(vk_tokens.get(str(call.from_user.id))).session
user = vk.API(session).users.get(user_ids=call.data, fields=[])[0]
@ -243,6 +265,16 @@ def dialogs_command(message):
create_markup(message, message.from_user.id, 0)
@bot.message_handler(commands=['search'])
def search_command(message):
markup = types.ForceReply(selective=False)
if telebot.util.extract_arguments(message.text):
search_users(message, telebot.util.extract_arguments(message.text))
else:
bot.send_message(message.from_user.id, '<b>Поиск беседы</b> 🔍',
parse_mode='HTML', reply_markup=markup).wait()
@bot.message_handler(commands=['stop'])
def stop_command(message):
if not check_thread(message.from_user.id):
@ -255,6 +287,9 @@ def stop_command(message):
@bot.message_handler(commands=['start'])
def start_command(message):
if check_thread(message.from_user.id):
mark = types.InlineKeyboardMarkup()
login = types.InlineKeyboardButton('ВХОД', url=link)
mark.add(login)
bot.send_message(message.from_user.id,
'Привет, этот бот поможет тебе общаться ВКонтакте, войди по кнопке ниже'
' и отправь мне то, что получишь в адресной строке.',
@ -590,6 +625,9 @@ def reply_text(message):
else:
bot.send_message(message.from_user.id, 'Вход уже выполнен!\n/stop для выхода.').wait()
return
if message.reply_to_message and message.reply_to_message.text == 'Поиск беседы 🔍':
search_users(message, message.text)
return
try:
vk_sender(message, send_text)
@ -599,6 +637,7 @@ def reply_text(message):
print('Error: {}'.format(traceback.format_exc()))
# bot.polling(none_stop=True)
class WebhookServer(object):
# index равнозначно /, т.к. отсутствию части после ip-адреса (грубо говоря)
@cherrypy.expose

View File

@ -22,8 +22,8 @@ class VkPolling:
updates = []
try:
updates = vk_user.get_new_messages()
except Exception:
print('Error: {}'.format(traceback.format_exc()))
except requests.exceptions.ReadTimeout as e:
print('Error: {}'.format(e))
if updates:
handle_updates(vk_user, bot, chat_id, updates)
for i in range(50):