F
F
fufa_me2021-05-10 14:39:37
Bots
fufa_me, 2021-05-10 14:39:37

Message chain in Vkbottle PYTHON - how to do it?

Is it possible to make a message chain in a VK bot using the VKBOTTLE library?
I tried to understand finite automata, but I didn’t learn anything sensible -

the code itself doesn’t work:

bot = Bot("efbc74d441***********9477***************** *******6f0edabf501759ff6c63bae5623ce127099") @ bot.on.private_message

(text='Start')
async def col1(message:Message):
await message.answer("First message in the chain")

message and the second response will run
@bot.on.private_message(text='msg')
async def col2(message:Message, msg):
await message.answer("Second message in the chain")

bot.run_forever()

memory

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Apache, 2021-12-21
@igor_89squad

Hey! As I understand you need states.
Information about them can be found here and on YouTube, so enter vkbottle states.

from vkbottle import CtxStorage, BaseStateGroup
from vkbottle.bot import Bot, Message

"""BOT"""
bot = Bot(token="TOKEN")
# ctx будет нужен для сохранения ответов.
ctx = CtxStorage()

# class в данном случае нужен для пошагового выполнения программы.
class Reg(BaseStateGroup)
     NAME=0
     AGE=1
     ABOUT=2
     END=3 #END я добавляю чтобы стейт останавливался.

@bot.on.private_message(lev="/reg")
async def Name(message: Message):
     await bot.state_dispenser.set(message.peer_id, Reg.NAME) # Тут мы вторым аргументом предает Reg.Name, NAME у нас выступит как первый шаг.
     return "Введи свое имя..." #Это сообщение отправиться пользователю, после ему надо будет ответить.
@bot.on.private_message(state=Reg.NAME) #Проще говоря, когда мы вводим state=Reg.NAME,  мы даем понять боту эта функция включится после активации Reg.NAME, мы его активировали в предыдущем блоке
async def Age(message: Message):
     ctx.set('name', message.text) # Мы записываем ответ пользователя под ключом name
     await bot.state_dispenser.set(message.peer_id, Reg.AGE) 
     return "Сколько вам лет?"
@bot.on.private_message(state=Reg.AGE)
async def About(message: Message):
     ctx.set('age', message.text) 
     await bot.state_dispenser.set(message.peer_id, Reg.ABOUT) 
     return "Расскажи о себе...."
@bot.on.private_message(state=Reg.ABOUT)
async def finally(message: Message):
     ctx.set('about', message.text) 
     await message.answer(f"Вот твои данные:\n{ctx.get('name')}\n{ctx.get('age')}\n{ctx.get('about')}")
     return "Ваши данные сохранены"

I hope I explained myself clearly, if there are mistakes write!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question