Example #1
0
def test_slack_no_metadata():
    from rasa.core.channels.slack import SlackInput

    input_channel = SlackInput(slack_token="YOUR_SLACK_TOKEN",
                               slack_channel="YOUR_SLACK_CHANNEL")

    r = Mock()
    metadata = input_channel.get_metadata(request=r)
    assert metadata == {}
Example #2
0
def test_slack_form_metadata():
    from rasa.core.channels.slack import SlackInput

    user = "******"
    channel = "channel1"
    authed_user = "******"
    ts = "1579802617.000800"
    header = {"content-type": "application/x-www-form-urlencoded"}
    payload = {
        "type": "block_actions",
        "user": {
            "id": authed_user,
            "username": user,
            "name": "name"
        },
        "channel": {
            "id": channel
        },
        "message": {
            "type":
            "message",
            "text":
            "text",
            "user":
            authed_user,
            "ts":
            ts,
            "blocks": [{
                "type":
                "actions",
                "block_id":
                "XXXXX",
                "elements": [{
                    "type": "button",
                    "action_id": "XXXXX",
                    "text": {
                        "type": "plain_text",
                        "text": "text"
                    },
                    "value": "value",
                }],
            }],
        },
    }
    form_event = {"payload": [json.dumps(payload)]}

    input_channel = SlackInput(slack_token="YOUR_SLACK_TOKEN",
                               slack_channel="YOUR_SLACK_CHANNEL")

    r = Mock()
    r.form = form_event
    r.headers = header
    metadata = input_channel.get_metadata(request=r)
    assert metadata["out_channel"] == channel
    assert metadata["users"][0] == authed_user
    assert metadata["thread_id"] == ts
Example #3
0
def test_slack_metadata():
    from rasa.core.channels.slack import SlackInput

    user = "******"
    channel = "channel1"
    authed_users = ["XXXXXXX", "YYYYYYY", "ZZZZZZZ"]
    ts = "1579802617.000800"
    header = {"content-type": "application/json"}
    direct_message_event = {
        "authed_users": authed_users,
        "event": {
            "client_msg_id":
            "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
            "type":
            "message",
            "text":
            "hello world",
            "user":
            user,
            "ts":
            ts,
            "team":
            "XXXXXXXXX",
            "blocks": [{
                "type":
                "rich_text",
                "block_id":
                "XXXXX",
                "elements": [{
                    "type": "rich_text_section",
                    "elements": [{
                        "type": "text",
                        "text": "hi"
                    }],
                }],
            }],
            "channel":
            channel,
            "event_ts":
            "1579802617.000800",
            "channel_type":
            "im",
        },
    }

    input_channel = SlackInput(slack_token="YOUR_SLACK_TOKEN",
                               slack_channel="YOUR_SLACK_CHANNEL")

    r = Mock()
    r.json = direct_message_event
    r.headers = header
    metadata = input_channel.get_metadata(request=r)
    assert metadata["out_channel"] == channel
    assert metadata["users"] == authed_users
    assert metadata["thread_id"] == ts
Example #4
0
def test_slack_init_three_parameters():
    from rasa.core.channels.slack import SlackInput

    ch = SlackInput("xoxb-test", "test", use_threads=True)
    assert ch.slack_token == "xoxb-test"
    assert ch.slack_channel == "test"
    assert ch.use_threads is True
Example #5
0
def test_slack_channel():
    with mock.patch.object(sanic.Sanic, "run", fake_sanic_run):
        # START DOC INCLUDE
        from rasa.core.channels.slack import SlackInput
        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 = SlackInput(
            slack_token="YOUR_SLACK_TOKEN",
            # this is the `bot_user_o_auth_access_token`
            slack_channel="YOUR_SLACK_CHANNEL"
            # the name of your channel to which the bot posts (optional)
        )

        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("slack_webhook.health").startswith(
            "/webhooks/slack")
        assert routes_list.get("slack_webhook.webhook").startswith(
            "/webhooks/slack/webhook")
Example #6
0
def test_slack_metadata_missing_keys():
    from rasa.core.channels.slack import SlackInput
    from sanic.request import Request

    channel = "channel1"
    direct_message_event = {
        "event": {
            "client_msg_id":
            "XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
            "type":
            "message",
            "text":
            "hello world",
            "ts":
            "1579802617.000800",
            "team":
            "XXXXXXXXX",
            "blocks": [{
                "type":
                "rich_text",
                "block_id":
                "XXXXX",
                "elements": [{
                    "type": "rich_text_section",
                    "elements": [{
                        "type": "text",
                        "text": "hi"
                    }],
                }],
            }],
            "channel":
            channel,
            "event_ts":
            "1579802617.000800",
            "channel_type":
            "im",
        }
    }

    input_channel = SlackInput(slack_token="YOUR_SLACK_TOKEN",
                               slack_channel="YOUR_SLACK_CHANNEL")

    r = Mock()
    r.json = direct_message_event
    metadata = input_channel.get_metadata(request=r)
    assert metadata["users"] is None
    assert metadata["out_channel"] == channel
Example #7
0
def test_slack_message_sanitization():
    from rasa.core.channels.slack import SlackInput

    test_uid = 17213535
    target_message_1 = "You can sit here if you want"
    target_message_2 = "Hey, you can sit here if you want !"
    target_message_3 = "Hey, you can sit here if you want!"
    target_message_4 = "convert garbled url to vicdb-f.net"
    target_message_5 = "convert multiple garbled url to vicdb-f.net. Also eemdb-p.net"

    uid_token = f"<@{test_uid}>"
    raw_messages = [
        test.format(uid=uid_token)
        for test in [
            "You can sit here {uid} if you want{uid}",
            "{uid} You can sit here if you want{uid} ",
            "{uid}You can sit here if you want {uid}",
            # those last cases may be disputable
            # as we're virtually altering the entered text,
            # but this seem to be the correct course of action
            # (to be decided)
            "You can sit here{uid}if you want",
            "Hey {uid}, you can sit here if you want{uid}!",
            "Hey{uid} , you can sit here if you want {uid}!",
            "convert garbled url to <http://vicdb-f.net|vicdb-f.net>",
            "convert multiple garbled url to <http://vicdb-f.net|vicdb-f.net>. Also <http://eemdb-p.net|eemdb-p.net>",
        ]
    ]

    target_messages = [
        target_message_1,
        target_message_1,
        target_message_1,
        target_message_1,
        target_message_2,
        target_message_3,
        target_message_4,
        target_message_5,
    ]

    sanitized_messages = [
        SlackInput._sanitize_user_message(message, [test_uid])
        for message in raw_messages
    ]

    # no message that is wrongly sanitized please
    assert (
        len(
            [
                sanitized
                for sanitized, target in zip(sanitized_messages, target_messages)
                if sanitized != target
            ]
        )
        == 0
    )
Example #8
0
def test_is_slack_message_true():
    from rasa.core.channels.slack import SlackInput

    event = {
        "type": "message",
        "channel": "C2147483705",
        "user": "******",
        "text": "Hello world",
        "ts": "1355517523",
    }
    payload = json.dumps({"event": event})
    slack_message = json.loads(payload)
    assert SlackInput._is_user_message(slack_message) is True
Example #9
0
def test_is_slack_message_true():
    from rasa.core.channels.slack import SlackInput

    event = {
        'type': 'message',
        'channel': 'C2147483705',
        'user': '******',
        'text': 'Hello world',
        'ts': '1355517523'
    }
    payload = json.dumps({'event': event})
    slack_message = json.loads(payload)
    assert SlackInput._is_user_message(slack_message) is True
Example #10
0
def test_slack_message_sanitization():
    from rasa.core.channels.slack import SlackInput

    test_uid = 17213535
    target_message_1 = "You can sit here if you want"
    target_message_2 = "Hey, you can sit here if you want !"
    target_message_3 = "Hey, you can sit here if you want!"

    uid_token = "<@{}>".format(test_uid)
    raw_messages = [
        test.format(uid=uid_token)
        for test in [
            "You can sit here {uid} if you want{uid}",
            "{uid} You can sit here if you want{uid} ",
            "{uid}You can sit here if you want {uid}",
            # those last cases may be disputable
            # as we're virtually altering the entered text,
            # but this seem to be the correct course of action
            # (to be decided)
            "You can sit here{uid}if you want",
            "Hey {uid}, you can sit here if you want{uid}!",
            "Hey{uid} , you can sit here if you want {uid}!",
        ]
    ]

    target_messages = [
        target_message_1,
        target_message_1,
        target_message_1,
        target_message_1,
        target_message_2,
        target_message_3,
    ]

    sanitized_messages = [
        SlackInput._sanitize_user_message(message, [test_uid])
        for message in raw_messages
    ]

    # no message that is wrongly sanitized please
    assert (
        len(
            [
                sanitized
                for sanitized, target in zip(sanitized_messages, target_messages)
                if sanitized != target
            ]
        )
        == 0
    )
Example #11
0
def test_slack_channel():
    # START DOC INCLUDE
    from rasa.core.channels.slack import SlackInput

    input_channel = SlackInput(
        slack_token="YOUR_SLACK_TOKEN",
        # this is the `bot_user_o_auth_access_token`
        slack_channel="YOUR_SLACK_CHANNEL"
        # the name of your channel to which the bot posts (optional)
    )

    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["slack_webhook.health"].startswith("/webhooks/slack")
    assert routes_list["slack_webhook.webhook"].startswith(
        "/webhooks/slack/webhook")
Example #12
0
def test_slack_channel():
    # START DOC INCLUDE
    from rasa.core.channels.slack import SlackInput

    input_channel = SlackInput(
        # this is the Slack Bot Token
        slack_token="YOUR_SLACK_TOKEN",
        # the name of your channel to which the bot posts (optional)
        slack_channel="YOUR_SLACK_CHANNEL",
        # signing secret from slack to verify incoming webhook messages
        slack_signing_secret="YOUR_SIGNING_SECRET",
    )

    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["slack_webhook.health"].startswith("/webhooks/slack")
    assert routes_list["slack_webhook.webhook"].startswith("/webhooks/slack/webhook")
Example #13
0
def test_is_slack_message_none():
    from rasa.core.channels.slack import SlackInput

    payload = {}
    slack_message = json.loads(json.dumps(payload))
    assert SlackInput._is_user_message(slack_message) is None
Example #14
0
def test_slack_init_two_parameters():
    from rasa.core.channels.slack import SlackInput

    ch = SlackInput("xoxb-test", "test")
    assert ch.slack_token == "xoxb-test"
    assert ch.slack_channel == "test"
Example #15
0
def test_slack_init_one_parameter():
    from rasa.core.channels.slack import SlackInput

    ch = SlackInput("xoxb-test")
    assert ch.slack_token == "xoxb-test"
    assert ch.slack_channel is None
Example #16
0
from rasa.core.channels.slack import SlackInput
from rasa.core.agent import Agent
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.utils.endpoints import EndpointConfig

# Replace <model_directory> with your models directory
nlu_interpreter = RasaNLUInterpreter('./models/model/nlu')
# Load agent with created models and connect to action server endpoint, replace <action_server_endpoint> with your endpoint
agent = Agent.load(
    './models/model',
    interpreter=nlu_interpreter,
    action_endpoint=EndpointConfig(url='http://localhost:5005/webhook'))

input_channel = SlackInput(
    # this is the `bot_user_o_auth_access_token`
    slack_token="xoxb-994655275015-992801903680-jqB9xEbzEcAU3bBIdoF0c470",
    # slack_channel="#general"
    # the name of your channel to which the bot posts (optional)
)

s = agent.handle_channels([input_channel], 5004)
Example #17
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 20 15:36:19 2019

@author: rajat-mehta
"""

from rasa.core.channels.slack import SlackInput
from rasa.core.agent import Agent
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.utils.endpoints import EndpointConfig

# Replace <model_directory> with your models directory
nlu_interpreter = RasaNLUInterpreter('./models/nlu')
# Load agent with created models and connect to action server endpoint, replace <action_server_endpoint> with your endpoint
agent = Agent.load(
    './models/',
    interpreter=nlu_interpreter,
    action_endpoint=EndpointConfig('http://*****:*****@jasper_muj_bot"
    # the name of your channel to which the bot posts (optional)
)
s = agent.handle_channels([input_channel], 5005)