def cb(event_loop: asyncio.AbstractEventLoop): """ :param event_loop: :rtype: Chatbase """ _chatbase = Chatbase(CHATBASE_TOKEN, CHATBOT_PLATFORM, loop=event_loop) yield _chatbase event_loop.run_until_complete(_chatbase.close())
def cb(event_loop: asyncio.AbstractEventLoop): """ Chatbase fixture with pool mode """ _chatbase = Chatbase(CHATBASE_TOKEN, CHATBOT_PLATFORM, loop=event_loop, pool_size=5) yield _chatbase event_loop.run_until_complete(_chatbase.close())
loop = asyncio.get_event_loop() loop.set_task_factory(context.task_factory) def get_proxy_data(): if config.PROXY_URL and config.PROXY_LOGIN and config.PROXY_PASSWORD: proxy = config.PROXY_URL if proxy.startswith('socks5'): import aiosocksy logger.info('Socks5 proxy enabled.') proxy_auth = aiosocksy.Socks5Auth(login=config.PROXY_LOGIN, password=config.PROXY_PASSWORD) else: import aiohttp logger.info('HTTP proxy enabled.') proxy_auth = aiohttp.BasicAuth(login=config.PROXY_LOGIN, password=config.PROXY_PASSWORD) else: logger.info('Proxy disabled.') proxy = None proxy_auth = None return proxy, proxy_auth # vars and instances url, auth = get_proxy_data() bot = Bot(token=config.TELEGRAM_TOKEN, loop=loop, proxy=url, proxy_auth=auth, parse_mode=types.ParseMode.HTML) dp = Dispatcher(bot, storage=MemoryStorage(), run_tasks_by_default=True) cb = Chatbase(api_key=config.CHATBASE_KEY, loop=loop, platform='Telegram', task_mode=True, pool_size=config.CHATBASE_POOL_SIZE) moder = Moderator(bot, cb)
import asyncio from aiochatbase import Chatbase API_KEY = 'paste-your-api-key-token-here' PLATFORM = 'Telegram' loop = asyncio.get_event_loop() cb = Chatbase(api_key=API_KEY, platform=PLATFORM, loop=loop) async def send_messages(): msg_1 = await cb.prepare_message('123456', 'test bulk') msg_2 = await cb.prepare_message('987654', 'test bulk') msg_3 = await cb.prepare_message('102938', 'test bulk') messages_list = [msg_1, msg_2, msg_3] result = await cb.register_messages(messages_list) print(f'Registered messages with ids: {result}.') loop.run_until_complete(send_messages()) loop.run_until_complete(cb.close()) loop.close()
async def test_cb_init_without_loop(event_loop): chatbase = Chatbase(CHATBASE_TOKEN, CHATBOT_PLATFORM) await chatbase.close()