A
A
Andrew_Kartoshka2019-12-24 15:15:19
Python
Andrew_Kartoshka, 2019-12-24 15:15:19

vk api. How to make the bot work in two (or more) conversations at the same time?

The bot does not want to work in two (or more) conversations at the same time. When you exclude him from the first, he starts working in the second. As I understand it, the problem is in the while True loop, the code is simply not executed after it. How can I stop it smartly and in time? In theory, the spam cycle should be infinite.

def write_msg(chat_id, random_id, message):
    vk.method('messages.send', {'chat_id': chat_id, "random_id" : random_id, "message" : message})

def write_stick_msg(chat_id, random_id, sticker_id):
    vk.method('messages.send', {'chat_id': chat_id, "random_id" : random_id, "sticker_id" : sticker_id})

while True:
    try:
        for event in longpoll.listen():
            if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat:
                print("Ok")
                event_text = event.object.text.lower()
                if event_text == "[club190004487|церковь картошки], помощь":
                    write_msg(event.chat_id, event.object.random_id, "Вы вызвали помощь, доступные команды:\n1. Служба\n2. Смайлики\n3. Стикеры\n4...в разработке")
                elif event_text == "[club190004487|церковь картошки], служба":
                    write_msg(event.chat_id, event.object.random_id, "Атеисту *** полбу! КАРТОШКИ СОБИРАЕМСЯ!!!\n*спам машина запускается*")
                    write_msg(event.chat_id, event.object.random_id, "3")
                    write_msg(event.chat_id, event.object.random_id, "2")
                    write_msg(event.chat_id, event.object.random_id, "1")
                    while True:
                        write_msg(event.chat_id, event.object.random_id, spam_molitva)
                        write_msg(event.chat_id, event.object.random_id, spam_song)
                        write_msg(event.chat_id, event.object.random_id, spam_molitva)
                        write_msg(event.chat_id, event.object.random_id, "Картошка БОГ")
                        write_msg(event.chat_id, event.object.random_id, "Картошка БОГ")
                        write_msg(event.chat_id, event.object.random_id, "Картошка БОГ")
                elif event_text == "[club190004487|церковь картошки], смайлики":
                    write_msg(event.chat_id, event.object.random_id, """Атеисту *** полбу! КАРТОШКИ СОБИРАЕМСЯ!!!\n*щас будет *** смайликов*""")
                    write_msg(event.chat_id, event.object.random_id, "3")
                    write_msg(event.chat_id, event.object.random_id, "2")
                    write_msg(event.chat_id, event.object.random_id, "1")
                    while True:
                        write_msg(event.chat_id, event.object.random_id, spam_emoji)
                elif event_text == "[club190004487|церковь картошки], стикеры": 
                    write_msg(event.chat_id, event.object.random_id, """Атеисту *** полбу! КАРТОШКИ СОБИРАЕМСЯ!!!\n*щас будет *** стикеров*""")
                    write_msg(event.chat_id, event.object.random_id, "3")
                    write_msg(event.chat_id, event.object.random_id, "2")
                    write_msg(event.chat_id, event.object.random_id, "1")
                    while True:
                        write_stick_msg(event.chat_id, event.object.random_id, str(random.randint(7674, 7721)))
    except Exception as read_timeout:
        continue
    except Exception as permission_denied:
        break

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2019-12-24
@anerev

I think it is possible to output part of the code with the flood into a separate function that will receive a chat ID, and in the "bot" itself, this function in the for loop from the list in which there will be chat id conversations in which the flood is enabled. As for the shutdown, you can add a longpoll request to the VC in the while loop, which will simply check whether the exit command has arrived.

C
che_aa, 2019-12-26
@che_aa

Handle the 'new_message' event and extract the necessary data from the VK server response

event = vk_otvet['event']
message = vk_otvet['message']
if event == 'new_message':
    if message == '1':
      vk.method = send.message
...

And so add the necessary messages, and process them already

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question