示例#1
0
def test_telegram_channel():
    # START DOC INCLUDE
    from rasa.core.channels.telegram import TelegramInput
    from rasa.core.agent import Agent
    from rasa.core.interpreter import RegexInterpreter

    # load your trained agent
    agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter())

    input_channel = TelegramInput(
        # you get this when setting up a bot
        access_token="123:YOUR_ACCESS_TOKEN",
        # this is your bots username
        verify="YOUR_TELEGRAM_BOT",
        # the url your bot should listen for messages
        webhook_url="YOUR_WEBHOOK_URL",
    )

    s = agent.handle_channels([input_channel], 5004)
    # END DOC INCLUDE
    # the above marker marks the end of the code snipped included
    # in the docs
    routes_list = utils.list_routes(s)
    assert routes_list.get("telegram_webhook.health").startswith("/webhooks/telegram")
    assert routes_list.get("telegram_webhook.message").startswith(
        "/webhooks/telegram/webhook"
    )
示例#2
0
def test_telegram_channel():
    # telegram channel will try to set a webhook, so we need to mock the api
    with mock.patch.object(sanic.Sanic, 'run', fake_sanic_run):
        httpretty.register_uri(
            httpretty.POST,
            'https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/setWebhook',
            body='{"ok": true, "result": {}}')

        httpretty.enable()
        # START DOC INCLUDE
        from rasa.core.channels.telegram import TelegramInput
        from rasa.core.agent import Agent
        from rasa.core.interpreter import RegexInterpreter

        # load your trained agent
        agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter())

        input_channel = TelegramInput(
            # you get this when setting up a bot
            access_token="123:YOUR_ACCESS_TOKEN",
            # this is your bots username
            verify="YOUR_TELEGRAM_BOT",
            # the url your bot should listen for messages
            webhook_url="YOUR_WEBHOOK_URL")

        s = agent.handle_channels([input_channel], 5004)
        # END DOC INCLUDE
        # the above marker marks the end of the code snipped included
        # in the docs
        routes_list = utils.list_routes(s)
        assert routes_list.get("telegram_webhook.health").startswith(
            "/webhooks/telegram")
        assert routes_list.get("telegram_webhook.message").startswith(
            "/webhooks/telegram/webhook")
        httpretty.disable()
示例#3
0
def test_telegram_channel_raise_rasa_exception_webhook_not_set():
    from rasa.core.channels.telegram import TelegramInput

    input_channel = TelegramInput(
        # you get this when setting up a bot
        access_token="123:YOUR_ACCESS_TOKEN",
        # this is your bots username
        verify="YOUR_TELEGRAM_BOT",
        # the url your bot should listen for messages
        webhook_url="",
    )

    with pytest.raises(RasaException) as e:
        rasa.core.run.configure_app([input_channel], port=5004)

    assert "Failed to set channel webhook:" in str(e.value)
示例#4
0
def test_telegram_channel():
    # START DOC INCLUDE
    from rasa.core.channels.telegram import TelegramInput

    input_channel = TelegramInput(
        # you get this when setting up a bot
        access_token="123:YOUR_ACCESS_TOKEN",
        # this is your bots username
        verify="YOUR_TELEGRAM_BOT",
        # the url your bot should listen for messages
        webhook_url="YOUR_WEBHOOK_URL",
    )

    s = rasa.core.run.configure_app([input_channel], port=5004)
    # END DOC INCLUDE
    # the above marker marks the end of the code snipped included
    # in the docs
    routes_list = utils.list_routes(s)
    assert routes_list.get("telegram_webhook.health").startswith("/webhooks/telegram")
    assert routes_list.get("telegram_webhook.message").startswith(
        "/webhooks/telegram/webhook"
    )
示例#5
0
def test_handling_of_telegram_user_id():
    # telegram channel will try to set a webhook, so we need to mock the api

    httpretty.register_uri(
        httpretty.POST,
        "https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/setWebhook",
        body='{"ok": true, "result": {}}',
    )

    # telegram will try to verify the user, so we need to mock the api
    httpretty.register_uri(
        httpretty.GET,
        "https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/getMe",
        body='{"result": {"id": 0, "first_name": "Test", "is_bot": true, '
        '"username": "******"}}',
    )

    # The channel will try to send a message back to telegram, so mock it.
    httpretty.register_uri(
        httpretty.POST,
        "https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/sendMessage",
        body='{"ok": true, "result": {}}',
    )

    httpretty.enable()

    from rasa.core.channels.telegram import TelegramInput
    from rasa.core.agent import Agent
    from rasa.core.interpreter import RegexInterpreter

    # load your trained agent
    agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter())

    input_channel = TelegramInput(
        # you get this when setting up a bot
        access_token="123:YOUR_ACCESS_TOKEN",
        # this is your bots username
        verify="YOUR_TELEGRAM_BOT",
        # the url your bot should listen for messages
        webhook_url="YOUR_WEBHOOK_URL",
    )

    import rasa.core

    app = Sanic(__name__)
    app.agent = agent
    rasa.core.channels.channel.register([input_channel],
                                        app,
                                        route="/webhooks/")

    data = {
        "message": {
            "chat": {
                "id": 1234,
                "type": "private"
            },
            "text": "Hello",
            "message_id": 0,
            "date": 0,
        },
        "update_id": 0,
    }
    test_client = app.test_client
    test_client.post(
        "/webhooks/telegram/webhook",
        data=json.dumps(data),
        headers={"Content-Type": "application/json"},
    )

    assert agent.tracker_store.retrieve("1234") is not None
    httpretty.disable()