fsm = FiniteStateMachine() bot.router.registrar.add_default_filter(StateFilter(fsm, ..., ..., always_false=True)) bot.router.registrar.add_default_filter( EventTypeFilter(BotEventType.MESSAGE_NEW.value) ) # we don't want to write it in all handlers. class MyState: name = State("name") age = State("age") # starting interview (has default filter that will reject messages if state exists) @bot.message_handler(bot.text_filter("start")) async def simple_handler(event: BotEvent): await fsm.set_state(event=event, state=MyState.name, for_what=ForWhat.FOR_USER) return "Input your name" # exiting interview (work with any state because of `state=ANY_STATE`) @bot.message_handler( bot.text_filter("exit"), StateFilter(fsm=fsm, state=ANY_STATE, for_what=ForWhat.FOR_USER) ) async def simple_handler2(event: BotEvent): if await fsm.get_data(event, for_what=ForWhat.FOR_USER) is not None: await fsm.finish(event=event, for_what=ForWhat.FOR_USER) return "You are quited!"
@execute def get_subs_names(api: APIOptionsRequestContext, group_id: int): subs_names = [] subscribers = api.groups.getMembers(group_id=group_id) subscribers = subscribers.items for sub_id in subscribers: subscriber_data = api.users.get(user_ids=sub_id) subs_names.append(subscriber_data[0].first_name) return subs_names @bot.message_handler(bot.text_filter("follow")) async def simple(event: bot.SimpleBotEvent): """ Get name of each subscriber """ check_group = 191949777 print(get_subs_names.build(api=event.api_ctx, group_id=check_group)) """ var subs_names=[]; var subscribers=API.groups.getMembers({group_id:1}); var subscribers=subscribers.items; while(subscribers.length > 0){ var sub_id=subscribers.pop(); var subscriber_data=API.users.get({user_ids:sub_id});
for u in counter: if u.isdigit(): try: name = l[int(u) - 1][1] db.delete(db, user_id, status[0][1], name) if status[0][1] == 'keeps': text = 'Заметка удалена' elif status[0][1] == 'alarms': text = 'Напоминание удалено' await event.answer(text + '\n') except: continue @bot.message_handler(bot.text_filter(["заметки", '1'])) async def keeps(event: bot.SimpleBotEvent): user_id = event.object.object.message.peer_id print('-команда-заметки-' + str(user_id) + '-' + str(datetime.now().time())) load = db.load(db, user_id, 'keeps') text = 'Ваши заметки:\n' i = 1 for item in load: text += str(i) + '. ' + item[1] + ' - ' + item[2] + '\n' i += 1 if len(load) == 0: text = "Новых заметок нет" db.update(db, user_id, 'status', {'screen': 'keeps'}) await event.answer(text + '\n' + keephelp, keyboard=kbb.get_keyboard())
from vkwave.bots import SimpleLongPollBot, TaskManager bot = SimpleLongPollBot(tokens="MyToken", group_id=123456789) # or if you want do a lot of requests without 'to many requests' errors # bot = SimpleLongPollBot(tokens=["MyToken1", "MyToken2", "MyToken3"], group_id=123456789) @bot.message_handler(bot.text_filter("hello")) async def simple(event: bot.SimpleBotEvent): await event.answer("hello from vkwave!") @bot.message_handler(bot.command_filter(commands=["start"])) async def start(event: bot.SimpleBotEvent): user_data = (await bot.api_context.users.get( user_ids=event.object.object.message.peer_id)).response[0] user_name = user_data.first_name await event.answer(f"You just started, {user_name}") bot.run_forever() # or # task_manager = TaskManager() # task_manager.add_task(bot.run) # task_manager.run()
""" create many bots with the same functionality """ import asyncio from vkwave.bots import SimpleLongPollBot, TaskManager, ClonesBot bot = SimpleLongPollBot( tokens=["Bot0TOKEN"], group_id=444, ) @bot.message_handler(bot.text_filter("123")) async def simple(event: bot.SimpleBotEvent): await event.answer("HELLO") clones = ClonesBot(bot, SimpleLongPollBot("Bot1TOKEN", 192868628), SimpleLongPollBot("Bot2TOKEN", 172702125)) async def clone_request(): # clones.clones - тупл с клонами (SimpleLongPollUserBot или SimpleLongPollBot) # Запрос с первого клона print(clones.clones[0].api_context.users.get()) asyncio.get_event_loop().run_until_complete(clone_request()) clones.run_all_bots()
peer_id = event.object.object.message.peer_id namefile = generate.dict_to_file(generate.create_dict(peer_id)) doc = await DocUploader(api.get_context()).get_attachment_from_path(peer_id, namefile, title="Dictionary.txt") await event.answer(attachment=doc) time.sleep(2) path = os.path.join(os.path.abspath(os.path.dirname(__file__)), namefile) os.remove(path) @bot.message_handler(bot.command_filter('generate')) async def generate_random(event: SimpleBotEvent) -> str: peer_id = event.object.object.message.peer_id return generate.generate(peer_id, 20) @bot.message_handler(bot.text_filter("бот рандом")) async def generate_random(event: SimpleBotEvent) -> str: peer_id = event.object.object.message.peer_id return generate.generate(peer_id, 20) @bot.message_handler(bot.command_filter('say_random')) async def handle(event: SimpleBotEvent): peer_id = event.object.object.message.peer_id text = generate.generate(peer_id, 20) await send_voice_message(event, peer_id, text) @bot.message_handler(bot.text_filter("бот рандом скажи")) async def handle(event: SimpleBotEvent): peer_id = event.object.object.message.peer_id
user_full_name = f'{user_data.first_name} {user_data.last_name}' user_first_name = user_data.first_name text_from_message = message.object.object.message.text user_params = { 'data': user_data, 'id': user_id, 'first_name': user_first_name, 'full_name': user_full_name, 'text_from_message': text_from_message } return user_params @bot.message_handler(bot.text_filter(['начать', 'start', 'привет', 'hi'])) async def send_welcome(message: bot.SimpleBotEvent): user_data = await get_user_data(message) await db.add_new_user(user_id=user_data['id'], user_name=user_data['full_name']) await message.answer(temp_msgs.form_welcome_message( user_data['first_name']), keyboard=DEFAULT_MARKUP) @bot.message_handler(bot.text_filter('аудио ➡ текст')) async def tell_to_send_voice(event: SimpleBotEvent): await fsm.set_state(event=event, state=GetterVoice.voice_message, for_what=ForWhat.FOR_USER) await event.answer(temp_msgs.tell_send_voice(), keyboard=CANCEL_MARKUP)