from typing import Optional from vkbottle import Keyboard, Text, GroupTypes, GroupEventType, VKAPIError from vkbottle.bot import Bot, Message bot = Bot(os.environ["token"]) # Logging level can be set through .basicConfig(level=LOGGING_LEVEL) # but if you use loguru the instruction is different. # --- # If you use loguru you need to remove default logger and add new with # level specified logging level, visit https://github.com/Delgan/loguru/issues/138 logging.basicConfig(level=logging.INFO) # Documentation for keyboard builder > tools/keyboard KEYBOARD = Keyboard(one_time=True).add(Text("Съесть еще", {"cmd": "eat"})).get_json() EATABLE = ["мороженое", "штаны", "пальто"] # If you need to make handler respond for 2 different rule set you can # use double decorator like here it is or use filters (OrFilter here) @bot.on.message(text=["/съесть <item>", "/съесть"]) @bot.on.message(payload={"cmd": "eat"}) async def eat_handler(message: Message, item: Optional[str] = None): if item is None: item = random.choice(EATABLE) await message.answer(f"Ты съел <<{item}>>!", keyboard=KEYBOARD) # You can use raw_event to handle any event type, the advantage is # free dataclass, for example it can be dict if you have some problems
from vkbottle import Keyboard, KeyboardButtonColor, Text # Simplest way of generating keyboard is non-builder interface # Use <.raw()> to add raw # Use <.add(Action(...), COLOR)> to add button to the last raw # Use <.get_json()> to make keyboard sendable KEYBOARD_STANDARD = Keyboard(one_time=True, inline=False) KEYBOARD_STANDARD.add(Text("Button 1"), color=KeyboardButtonColor.POSITIVE) KEYBOARD_STANDARD.add(Text("Button 2")) KEYBOARD_STANDARD.row() KEYBOARD_STANDARD.add(Text("Button 3")) KEYBOARD_STANDARD = KEYBOARD_STANDARD.get_json() # add and row methods returns the instance of Keyboard # so you can use it as builder KEYBOARD_WITH_BUILDER = (Keyboard(one_time=True, inline=False).add( Text("Button 1"), color=KeyboardButtonColor.POSITIVE).add( Text("Button 2")).row().add(Text("Button 3")).get_json()) # Schema is another way to create keyboard # all fields except of color are read as action fields KEYBOARD_WITH_SCHEMA = (Keyboard(one_time=True, inline=False).schema([ [ { "label": "Button 1", "type": "text", "color": "positive" }, { "label": "Button 2", "type": "text"
from vkbottle import Keyboard, KeyboardButtonColor, Text from src.commands import * KEYBOARD_ENTRYPOINT = Keyboard(inline=False, one_time=True) KEYBOARD_ENTRYPOINT.add(Text(AdminOpenLock.button_name, payload={'cmd': AdminOpenLock.key}), color=KeyboardButtonColor.PRIMARY) KEYBOARD_ENTRYPOINT.add(Text(AdminCloseLock.button_name, payload={'cmd': AdminCloseLock.key}), color=KeyboardButtonColor.PRIMARY) KEYBOARD_ENTRYPOINT.row() for command in [GetPasswordCommand, HelpCommand]: KEYBOARD_ENTRYPOINT.add(Text(command.button_name, payload={'cmd': command.key}), color=KeyboardButtonColor.SECONDARY)
PREDMETS = (Keyboard(one_time=False, inline=False).add( Callback('Русск. яз. (база)', payload={ 'cmd': 'rusyas' })).add(Callback('Русск. яз. (профиль)', payload={ 'cmd': 'rusyas_p' })).row().add(Callback('Русск. лит.', payload={ 'cmd': 'ruslit' })).add(Callback('Бел. яз.', payload={ 'cmd': 'belyas' })).row().add(Callback('Бел. лит.', payload={ 'cmd': 'bellit' })).add(Callback('Математ. (база)', payload={ 'cmd': 'mat' })).row().add(Callback('Математ. (профиль)', payload={ 'cmd': 'mat_p' })).add(Callback('Биолог.', payload={ 'cmd': 'bio' })).row().add(Callback('Всем. истор.', payload={ 'cmd': 'vistor' })).add(Callback('Истор. Бел.', payload={ 'cmd': 'bistor' })).row().add(Callback('Обществ.', payload={ 'cmd': 'obsch' })).add(Callback('Информат.', payload={ 'cmd': 'info' })).row().add(Callback('Астроном.', payload={ 'cmd': 'astro' })).add(Callback('Физ.', payload={ 'cmd': 'phys' })).row().add(Callback('Хим.', payload={ 'cmd': 'him' })).add(Callback('Географ.', payload={ 'cmd': 'geo' })).row().add(Callback('Франц. яз. (Тараникова)', payload={ 'cmd': 'fra_t' })).add(Callback('Франц. яз. (Дорошкова)', payload={'cmd': 'fra_d'})).row().add( Callback('Франц. яз. ()', payload={'cmd': 'fra_'})).add( Callback('Нем. яз.', payload={'cmd': 'nem'})).get_json())
# Example of sending and receiving an event after pressing the Callback button # Documentation: https://vk.cc/aC9JG2 import logging import os from vkbottle import Callback, GroupEventType, GroupTypes, Keyboard, ShowSnackbarEvent from vkbottle.bot import Bot, Message bot = Bot(os.environ["TOKEN"]) logging.basicConfig(level=logging.INFO) KEYBOARD = (Keyboard(one_time=False).add( Callback("Callback-кнопка", payload={"cmd": "callback"})).get_json()) @bot.on.private_message(text="/callback") async def send_callback_button(message: Message): await message.answer("Лови!", keyboard=KEYBOARD) @bot.on.raw_event(GroupEventType.MESSAGE_EVENT, dataclass=GroupTypes.MessageEvent) async def handle_message_event(event: GroupTypes.MessageEvent): # event_data parameter accepts three object types # "show_snackbar" type await bot.api.messages.send_message_event_answer( event_id=event.object.event_id, user_id=event.object.user_id, peer_id=event.object.peer_id,
# Example of sending and receiving an event after pressing the Callback button # Documentation: https://dev.vk.com/api/bots/development/keyboard#Callback-кнопки import logging import os from vkbottle import Callback, GroupEventType, Keyboard from vkbottle.bot import Bot, Message, MessageEvent, rules bot = Bot(os.environ["TOKEN"]) logging.basicConfig(level=logging.INFO) KEYBOARD = (Keyboard(one_time=False, inline=True).add( Callback("Показать текст", payload={"cmd": "snackbar"})).row().add( Callback("Дата регистрации (tool42)", payload={"cmd": "app"})).row().add( Callback("Закрыть", payload={"cmd": "close"})).get_json()) @bot.on.private_message(text="/callback") async def send_callback_button(message: Message): await message.answer("Лови!", keyboard=KEYBOARD) @bot.on.raw_event( GroupEventType.MESSAGE_EVENT, MessageEvent, rules.PayloadRule({"cmd": "snackbar"}), ) async def show_snackbar(event: MessageEvent): await event.show_snackbar("Сейчас я исчезну")