Beispiel #1
0
    async def test():
        vk = VkontakteCallback("token")
        vk.updates_queue = asyncio.Queue()
        await vk.updates_queue.put("hey")

        submitted = []

        async def submit(arg):
            submitted.append(arg)

        await vk.acquire_updates(submit)

        assert submitted == ["hey"]
Beispiel #2
0
def test_callback_without_address():
    vk = VkontakteCallback("token")

    assert vk.api_token == "token"
    assert vk._address is None
    assert vk._address_path == "/"
    assert vk._server_path == "/"
Beispiel #3
0
def test_callback_settings():
    vk = VkontakteCallback("token", address="api.bot.vk/callback")

    assert vk.api_token == "token"
    assert vk._address == "https://api.bot.vk/callback"
    assert vk._address_path == "/callback"
    assert vk._server_path == "/callback"
Beispiel #4
0
    async def test():
        port = random.randint(9900, 9999)

        vk = VkontakteCallback("token", host="127.0.0.1", port=port)

        # Mini-start
        vk.updates_queue = asyncio.Queue()

        try:
            await vk.start_server()
        except OSError as e:
            if e.errno != 48:
                raise
            return await test()

        calls = []

        async def _request(*args, **kwargs):
            calls.append([args, kwargs])
            return {"code": "123"}

        vk.request = _request

        async with aiohttp.ClientSession() as session:
            async with session.post(f'http://127.0.0.1:{port}',
                                    json={
                                        "type": "confirmation",
                                        "group_id": 1,
                                    }) as resp:
                assert await resp.text() == "123"

        assert len(calls) == 1

        async with aiohttp.ClientSession() as session:
            async with session.post(f'http://127.0.0.1:{port}',
                                    json={
                                        "type": "raw_update",
                                        "object": "bruh"
                                    }) as resp:
                assert await resp.text() == "ok"

        assert len(calls) == 1
        assert vk.updates_queue.qsize() == 1

        await vk.stop_server()
Beispiel #5
0
    async def test():
        vk = VkontakteCallback("token")

        vk.start_server = noop
        vk._execute_loop = noop
        vk._update_group_data = noop
        vk.request = _request

        await vk.on_start(Kutana())

        assert vk.updates_queue
        assert len(calls) == 0
Beispiel #6
0
def add_backends(app, backends):
    for backend in backends or []:
        kwargs = {k: v for k, v in backend.items() if k != "kind"}
        name = kwargs.get("name")

        if name and app.get_backend(name):
            logger.logger.warning(f"Duplicated backend name: {name}")

        if backend["kind"] == "vk" and "address" in backend:
            app.add_backend(VkontakteCallback(**kwargs))

        elif backend["kind"] == "vk":
            app.add_backend(Vkontakte(**kwargs))

        elif backend["kind"] == "tg":
            app.add_backend(Telegram(**kwargs))

        else:
            logger.logger.warning(f"Unknown backend kind: {backend['kind']}")
Beispiel #7
0
import json
from kutana import Kutana, load_plugins
from kutana.backends import Vkontakte, VkontakteCallback, Telegram

# Import configuration
with open("config.json") as fh:
    config = json.load(fh)

# Create application
app = Kutana()

# Add backends to application
if "vk" in config:
    if "address" in config["vk"]:
        app.add_backend(
            VkontakteCallback(token=config["vk"]["token"],
                              address=config["vk"]["address"]))
    else:
        app.add_backend(Vkontakte(token=config["vk"]["token"]))

if "tg" in config:
    app.add_backend(Telegram(token=config["tg"]["token"]))

# Load and register plugins
app.add_plugins(load_plugins("plugins/"))

if __name__ == "__main__":
    # Run application
    app.run()