def test_no_exception_with_empty_response(requests_mock): excpected = '???' response = [{}] url = cfg.get('quote_api_url') + '/random_quotes?limit=1' requests_mock.get(url, json=response) response = quote_api.get_random_quote() assert excpected == response
def test_quote_can_be_parsed_from_response(requests_mock): test_quote = 'test_quote' response = [{'quote': test_quote}] url = cfg.get('quote_api_url') + '/random_quotes?limit=1' requests_mock.get(url, json=response) response = quote_api.get_random_quote() assert test_quote == response
async def test_quotelast_api_is_not_called_without_reply(requests_mock): url = cfg.get('quote_api_url') + '/rpc/quotelast' message_stub = MessageStub() message_stub.reply_to_message = None adapter = requests_mock.post(url) await quotelast_command.index(message_stub) assert adapter.called is False
async def test_quote_is_returned_on_command(requests_mock): expected = 'test quote' reply = [{'quote': expected}] url = cfg.get('quote_api_url') + '/random_quotes?limit=1' requests_mock.get(url, json=reply) message_stub = MessageStub() await quote_command.index(message_stub) assert message_stub.replied == expected
async def test_quotelast_api_is_called_from_handler(requests_mock): url = cfg.get('quote_api_url') + '/rpc/quotelast' message_stub = MessageStub() message_stub.reply_to_message = MessageStub() message_stub.reply_to_message.text = "test quote" adapter = requests_mock.post(url) await quotelast_command.index(message_stub) assert adapter.called
async def test_quotelast_is_filtered_by_rules(requests_mock, text): url = cfg.get('quote_api_url') + '/rpc/quotelast' message_stub = MessageStub() message_stub.reply_to_message = MessageStub() message_stub.reply_to_message.text = text adapter = requests_mock.post(url) await quotelast_command.index(message_stub) assert adapter.called is False
def test_quotelast_can_be_called(requests_mock): url = cfg.get('quote_api_url') + '/rpc/quotelast' adapter = requests_mock.post(url) quote_api.quotelast("test_channel", "test quote", "test_victim", "test_adder") assert adapter.called assert adapter.last_request.json() == { 'a_adder': 'test_adder', 'a_channel': 'test_channel', 'a_quote': 'test quote', 'a_victim': 'test_victim', }
import logging import os from aiogram import Bot, Dispatcher, executor from aiogram.dispatcher.filters.builtin import IDFilter from janisbot4.config import cfg from janisbot4 import plugin_loader TELEGRAM_API_TOKEN = cfg.get('telegram_api_token') logging.basicConfig( level=int(os.environ.get('JANISBOT_LOGLEVEL', logging.INFO))) bot = Bot(token=TELEGRAM_API_TOKEN) dp = Dispatcher(bot) idfilter = IDFilter(user_id=cfg.get('user_ids'), chat_id=cfg.get('chat_ids')) plugin_loader.register_plugins(plugin_loader.load_plugins("janisbot4/plugins"), dp, idfilter) if __name__ == '__main__': executor.start_polling(dp, skip_updates=True)
import requests from urllib.parse import quote as urlquote from janisbot4.config import cfg QUOTE_API_TOKEN = cfg.get('quote_api_token') QUOTE_API_URL = cfg.get('quote_api_url') HEADERS = {'Authorization': QUOTE_API_TOKEN} EMPTY_RESPONSE = '???' def quotelast(channel, quote, victim=None, adder=None): requests.post(f'{QUOTE_API_URL}/rpc/quotelast', headers=HEADERS, json=dict( a_channel=channel, a_victim=victim, a_adder=adder, a_quote=quote, )) def request(request_str): response = requests.get(f'{QUOTE_API_URL}/{request_str}', headers=HEADERS) return response.json() def get_random_quote(arguments=None): argumentstr = _parse_include_exclude(arguments)