示例#1
0
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()
示例#2
0
router = DefaultRouter()
logging.basicConfig(level=logging.INFO)
db.execute_query(connection,
                 """
CREATE TABLE IF NOT EXISTS messages( 
id INTEGER PRIMARY KEY AUTOINCREMENT,
peer_id INTEGER,
text TEXT NOT NULL,
from_id INTEGER,
name TEXT NOT NULL default unknown,
date TEXT NOT NULL default unknown);
""")


# help page
@bot.message_handler(bot.command_filter(commands=['help']))
async def command_help(event: SimpleBotEvent):
    help_text = "Текущие команды доступны: \n"
    for key in commands:
        help_text += "/" + key + ": "
        help_text += commands[key] + "\n"
    await event.answer(message=help_text)


@bot.message_handler(bot.command_filter(('get_messages', 'history')))
async def handle(event: SimpleBotEvent) -> str:
    peer_id = event.object.object.message.peer_id
    return db.get_messages(peer_id)


@bot.message_handler(bot.command_filter('get_dict'))