Exemplo n.º 1
0
def test_exception_using_same_port(create_bot):
    """ Starts two bots on the same port, the second should not start and raise
        and exception.
    """

    bot1 = create_bot(Bot(), HttpEndpoint(port=8046))

    with pytest.raises(Exception):
        # not using fixture because otherwise I won't get the exception
        bot2 = Bot()
        endpoint2 = HttpEndpoint(port=8046)
        bot2.add_endpoint(endpoint2)
        bot2.run()

    resp = send_to_http_bot(bot1, "/start")
    assert resp.status_code == 200

    resp = send_to_http_bot(bot2, "/start")
    assert resp is None

    bot1.stop()
Exemplo n.º 2
0
def test_expose_html_page(create_bot):
    """ Starting the bot, an HTML page is available in the root of the webserver
        showing a form to chat with the bot.
    """

    endpoint = HttpEndpoint(port=randint(8000, 9000))
    create_bot(Bot(), endpoint)

    address = "http://%s:%d/" % (endpoint.host, endpoint.port)

    response = requests.get(address)

    assert 'html' in response.text.lower()
Exemplo n.º 3
0
def test_uses_a_custom_port():
    """ Start the webserver on a port specified, then try to send messages to
        that port.
    """

    bot = Bot()
    endpoint = HttpEndpoint(port=8045)
    bot.add_endpoint(endpoint)
    bot.run()

    resp = send_to_http_bot(bot, "/start", port=8045)
    assert resp.status_code == 200

    bot.stop()
Exemplo n.º 4
0
def test_http_interface(create_bot):
    """ Test that the http interface uses the bot with the given bot class
    """
    class MyBot(Bot):
        "Reverse bot"

        def default_response(self, in_message):
            return in_message[::-1]

    bot = create_bot(MyBot(), HttpEndpoint(port=randint(8000, 9000)))

    test_messages = ["hello", "another message"]
    for message in test_messages:
        resp = send_to_http_bot(bot, message)

        assert resp.status_code == 200
        ret = json.loads(resp.text)
        assert ret["out_message"] == message[::-1]
Exemplo n.º 5
0
def test_http_command(create_bot):
    """ Test that the http interface correctly process commands
    """
    class MyBot(Bot):
        "Reverse bot, welcoming"

        def default_response(self, in_message):
            return in_message[::-1]

        @command
        def start(self):
            "Welcome the user as first thing!"
            return "Welcome!"

    bot = create_bot(MyBot(), HttpEndpoint(port=randint(8000, 9000)))

    resp = send_to_http_bot(bot, "/start")

    assert resp.status_code == 200
    ret = json.loads(resp.text)
    assert ret["out_message"] == "Welcome!"
Exemplo n.º 6
0
def test_returns_text_and_html(create_bot):
    """ Test that the endpoint response contains the same message in plain text
        and in HTML formatting (\n -> <br />)
    """
    class MyBot(Bot):
        "Echo bot"

        def default_response(self, in_message):
            return in_message

    bot = create_bot(MyBot(), HttpEndpoint(port=randint(8000, 9000)))

    resp = send_to_http_bot(
        bot, "Hello,\nit's me&myself.\nBut <me> it's not <myself>")

    assert resp.status_code == 200
    ret = json.loads(resp.text)
    assert ret[
        "out_message"] == "Hello,\nit's me&myself.\nBut <me> it's not <myself>"
    assert ret["out_message_html"] == "Hello,<br />it's me&amp;myself.<br />" + \
        "But &lt;me&gt; it's not &lt;myself&gt;"
Exemplo n.º 7
0
#!/usr/bin/env python3

import os
import logging

from eddie.endpoints import HttpEndpoint, TelegramEndpoint, TwitterEndpoint
from example_bot import ExampleBot

log_level = os.environ.get('BOT_LOGLEVEL', 'ERROR')
logging.basicConfig(level=log_level)

if __name__ == "__main__":
    bot = ExampleBot()

    bot.add_endpoint(HttpEndpoint(port=int(os.environ['PORT'])))

    bot.add_endpoint(TelegramEndpoint(token=os.environ['BOT_TG_TOKEN']))

    bot.add_endpoint(
        TwitterEndpoint(
            consumer_key=os.environ['BOT_TW_consumer_key'],
            consumer_secret=os.environ['BOT_TW_consumer_secret'],
            access_token=os.environ['BOT_TW_access_token'],
            access_token_secret=os.environ['BOT_TW_access_token_secret']))

    bot.run()

    logging.info("Serving...")