J
J
Just1n2019-05-08 14:13:05
Python
Just1n, 2019-05-08 14:13:05

How to set a prefix for a VKonakte chatbot in Python?

from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
import wikipediaapi

wiki_wiki = wikipediaapi.Wikipedia('ru')

vk_session = vk_api.VkApi(token='мой токен')
vk = vk_session.get_api()

longpoll = VkBotLongPoll(vk_session, id моего бота)

Wiki = ['Википедия', 'Wikipedia', 'Вики','Wiki','википедия','вики','wikipedia','wiki']

while True:
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW:
            if event.object.peer_id != event.object.from_id:
                if event.object.text.lower() in Wiki:
                    vk.messages.send(peer_id=event.object.peer_id, message='Введите запрос', random_id=0)
                    for event in longpoll.listen():
                        if event.type == VkBotEventType.MESSAGE_NEW and not event.from_me:
                            if event.object.peer_id != event.object.from_id:
                                page_py = wiki_wiki.page(event.object.text.lower())
                                if page_py.exists() == False:
                                    vk.messages.send(peer_id=event.object.peer_id, message='Ошибка:\nТакой страницы не существует', random_id=0)
                                if page_py.exists() == True:
                                    vk.messages.send(peer_id=event.object.peer_id, message='Обращайтесь\n' + str(page_py.title) + '\n' + str(page_py.summary), random_id=0)
                                break

I want to make it so that the bot doesn't request a request after the first message. We want the word "Wiki" to be the prefix, and the rest of the message without the prefix to become the query. For example "Wiki Internet" and the bot sends an article to the Internet. I don't know how to output part of the message without the prefix into a separate variable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2019-05-08
@Just1n

for count in range(len(Wiki)):
    if event.object.text.find(Wiki[count]) >= 0:
        True

for event in longpoll.listen() does not need to be inserted into While.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question