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
dp.setup_blueprint(invite_bp) dp.setup_blueprint(generate_bp) dp.setup_blueprint(info_bp) dp.setup_blueprint(clear_bp) dp.setup_blueprint(remember_bp) dp.setup_middleware(MessageCheckMiddleware()) if PRODUCTION: try: import starlette import aio_pika import uvicorn except ImportError: print("Для использования PRODUCTION мода вам нужно установить" " дополнительные библиотеки: starlette, aio_pika, uvicorn") exit(1) from vk.bot_framework.extensions import RabbitMQ dp.setup_extension(RabbitMQ) dp.run_extension("rabbitmq", vk=vk, queue_name="bot_queue") else: group_id = await get_group_id(vk) dp.run_polling(group_id) if __name__ == "__main__": if not os.path.exists("dialogs/"): os.mkdir("dialogs/") task_manager = TaskManager(vk.loop) task_manager.add_task(run) task_manager.run()
import logging from vk import types from vk import VK from vk.bot_framework import Dispatcher 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()
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) 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)
from vk import VK from vk.utils import TaskManager from vk.bot_framework import Dispatcher from vk.constants import JSON_LIBRARY import asyncio import logging from io import BytesIO from aiogtts import aiogTTS import aiohttp with open("config.json", "r") as config: config = JSON_LIBRARY.load(config) logging.basicConfig(level=config["logging_level"], format="%(asctime)s - %(levelname)s:: %(message)s", handlers=[ logging.FileHandler("{0}/{1}".format( config["logging_path"], config["logging_file"])), logging.StreamHandler() ]) vk = VK(config["token"]) task_manager = TaskManager(vk.loop) dp = Dispatcher(vk, config["group_id"]) api = vk.get_api() aiogtts = aiogTTS()
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()
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()
import logging import typing from vk import VK from vk.utils import TaskManager logging.basicConfig(level="DEBUG") token = "TOKEN" vk = VK(access_token=token) task_manager = TaskManager(vk.loop) error_dp = vk.error_dispatcher @error_dp.error_handler(error_code=1) async def handler(error: typing.Dict): logging.info("Exception with code '1' handled!") async def status_get(): resp = await vk.api_request("status.get") print(resp) if __name__ == "__main__": task_manager.add_task(status_get) task_manager.run() task_manager.close() # close event loop manually