from vk import VK from vk.utils import TaskManager import asyncio import logging logging.basicConfig(level="DEBUG") token = "TOKEN" vk = VK(access_token=token) task_manager = TaskManager(vk.loop) api = vk.get_api() async def send_message(): resp = await api.messages.send(peer_id=1, message="hello!", random_id=0) print(resp) if __name__ == "__main__": task_manager.add_task(send_message) task_manager.run() task_manager.close() # close event loop manually
from vk.utils import TaskManager logging.basicConfig(level="INFO") bot_token = "123" vk = VK(bot_token) gid = 123 task_manager = TaskManager(vk.loop) api = vk.get_api() dp = Dispatcher(vk, gid) @dp.message_handler(with_fwd_messages=True, count_fwd_messages=3) async def handle(message: types.Message, data: dict): await message.reply("Test!") @dp.message_handler(with_reply_message=True) async def handle_event(message: types.Message, data: dict): await message.reply("Hello!") async def run(): dp.run_polling() if __name__ == "__main__": task_manager.add_task(run) task_manager.run(auto_reload=True)
from vk import VK from vk.bot_framework import Dispatcher from vk.utils import TaskManager from shuecm.config import VK_GROUP_ID from shuecm.config import VK_TOKEN from shuecm.models.prestart import pre_start as pre_start_db logging.basicConfig(level="INFO") vk = VK(VK_TOKEN) dp = Dispatcher(vk, VK_GROUP_ID) async def run(): from shuecm.blueprints import info_bp dp.setup_blueprint(info_bp) from shuecm.middlewares import RegistrationMiddleware dp.setup_middleware(RegistrationMiddleware()) pre_start_db(vk.loop) dp.run_polling() if __name__ == "__main__": manager = TaskManager(vk.loop) manager.add_task(run) manager.run()
import asyncio import logging logging.basicConfig(level="DEBUG") token = "TOKEN" vk = VK(access_token=token) task_manager = TaskManager(vk.loop) async def status_get(): resp = await vk.api_request("status.get") print(resp) async def on_startup(): print("Started!") async def on_shutdown(): await vk.close() await asyncio.sleep(0.250) print("closed!") if __name__ == "__main__": task_manager.add_task(status_get) task_manager.run(on_shutdown=on_shutdown, on_startup=on_startup) task_manager.close() # close event loop manually
async def set_online(vk: VK): if config['lifetime_online_settings']['voip'] is True: return await vk.api_request('account.setOnline', {'voip': 1}) else: return await vk.api_request('account.setOnline', {'voip': 0}) async def set_status(vk: VK, status: str): return await vk.api_request('account.saveProfileInfo', {'status': status}) async def application_cycle(): data = await fetch_profile_info(application) logger.info(f'Authorized to vk.com/{data["screen_name"]}') pattern = config['status_pattern'] while True: _pattern = pattern.replace('%current_time%', str(datetime.datetime.now().astimezone(timezone(config['timezone'])).strftime(config['time_pattern']))) _pattern = _pattern.replace('%emoji_time%', utils.digits_to_emojis(str(datetime.datetime.now().astimezone(timezone(config['timezone'])).strftime(config['time_pattern'])))) await asyncio.sleep(0.2) if (await fetch_profile_info(application))["status"] != _pattern: await set_status(application, _pattern) if config['lifetime_online_settings']['enabled'] is True: await set_online(application) await asyncio.sleep(0.5) if __name__ == '__main__': application_manager = ApplicationManager(application.loop) application_manager.add_task(application_cycle) application_manager.run()