async def test_slackbot_send_attachment_with_text_threaded(): from rasa.core.channels.slack import SlackBot with aioresponses() as mocked: mocked.post( "https://www.slack.com/api/chat.postMessage", payload={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General", thread_id="DummyThread") attachment = SLACK_TEST_ATTACHMENT attachment["text"] = "Here is the summary:" await bot.send_attachment("ID", attachment) r = latest_request(mocked, "POST", "https://www.slack.com/api/chat.postMessage") assert r request_params = json_of_latest_request(r) assert request_params == { "channel": "General", "as_user": True, "attachments": [attachment], "thread_ts": "DummyThread", }
async def test_slackbot_send_attachment_withtext(): from rasa.core.channels.slack import SlackBot httpretty.register_uri(httpretty.POST, 'https://slack.com/api/chat.postMessage', body='{"ok":true,"purpose":"Testing bots"}') httpretty.enable() bot = SlackBot("DummyToken", "General") text = "Sample text" attachment = json.dumps([{ "fallback": "Financial Advisor Summary", "color": "#36a64f", "author_name": "ABE", "title": "Financial Advisor Summary", "title_link": "http://tenfactorialrocks.com", "image_url": "https://r.com/cancel/r12", "thumb_url": "https://r.com/cancel/r12", "actions": [{ "type": "button", "text": "\ud83d\udcc8 Dashboard", "url": "https://r.com/cancel/r12", "style": "primary" }, { "type": "button", "text": "\ud83d\udccb XL", "url": "https://r.com/cancel/r12", "style": "danger" }, { "type": "button", "text": "\ud83d\udce7 E-Mail", "url": "https://r.com/cancel/r123", "style": "danger" }], "footer": "Powered by 1010rocks", "ts": 1531889719 }]) await bot.send_attachment("ID", attachment, text) httpretty.disable() r = httpretty.latest_requests[-1] assert r.parsed_body == { 'channel': ['General'], 'as_user': ['True'], 'text': ['Sample text'], 'attachments': [attachment] }
async def test_slackbot_send_attachment_only(): from rasa.core.channels.slack import SlackBot responses.add( responses.POST, "https://slack.com/api/chat.postMessage", body='{"ok":true,"purpose":"Testing bots"}', ) bot = SlackBot("DummyToken", "General") attachment = { "fallback": "Financial Advisor Summary", "color": "#36a64f", "author_name": "ABE", "title": "Financial Advisor Summary", "title_link": "http://tenfactorialrocks.com", "image_url": "https://r.com/cancel/r12", "thumb_url": "https://r.com/cancel/r12", "actions": [ { "type": "button", "text": "\ud83d\udcc8 Dashboard", "url": "https://r.com/cancel/r12", "style": "primary", }, { "type": "button", "text": "\ud83d\udccb Download XL", "url": "https://r.com/cancel/r12", "style": "danger", }, { "type": "button", "text": "\ud83d\udce7 E-Mail", "url": "https://r.com/cancel/r12", "style": "danger", }, ], "footer": "Powered by 1010rocks", "ts": 1531889719, } await bot.send_attachment("ID", attachment) r = responses.calls[-1] assert r.request.body == { "channel": ["General"], "as_user": ["True"], "attachments": [json.dumps([attachment])], }
async def test_slackbot_send_custom_json_threaded(): from rasa.core.channels.slack import SlackBot with aioresponses() as mocked: mocked.post( "https://www.slack.com/api/chat.postMessage", payload={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General", thread_id="DummyThread") await bot.send_custom_json("ID", {"test_key": "test_value"}) r = latest_request(mocked, "POST", "https://www.slack.com/api/chat.postMessage") assert r request_params = json_of_latest_request(r) assert request_params == { "as_user": True, "channel": "General", "thread_ts": "DummyThread", "test_key": "test_value", }
async def test_slackbot_send_text(): from rasa.core.channels.slack import SlackBot with aioresponses() as mocked: mocked.post( "https://www.slack.com/api/chat.postMessage", payload={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General") await bot.send_text_message("ID", "my message") r = latest_request(mocked, "POST", "https://www.slack.com/api/chat.postMessage") assert r request_params = json_of_latest_request(r) assert request_params == { "as_user": True, "channel": "General", "text": "my message", "type": "mrkdwn", }
def test_slackbot_init_three_parameter(): from rasa.core.channels.slack import SlackBot bot = SlackBot("DummyToken", "General", thread_id="DummyThread") assert bot.client.token == "DummyToken" assert bot.slack_channel == "General" assert bot.thread_id == "DummyThread"
async def test_slackbot_send_image_url(): from rasa.core.channels.slack import SlackBot httpretty.register_uri( httpretty.POST, "https://slack.com/api/chat.postMessage", body='{"ok":true,"purpose":"Testing bots"}', ) httpretty.enable() bot = SlackBot("DummyToken", "General") url = "http://www.rasa.net" await bot.send_image_url("ID", url) httpretty.disable() r = httpretty.latest_requests[-1] assert r.parsed_body["as_user"] == ["True"] assert r.parsed_body["channel"] == ["General"] assert len(r.parsed_body["blocks"]) == 1 assert '"type": "image"' in r.parsed_body["blocks"][0] assert '"alt_text": "http://www.rasa.net"' in r.parsed_body["blocks"][0] assert '"image_url": "http://www.rasa.net"' in r.parsed_body["blocks"][0]
async def test_slackbot_send_text(): from rasa.core.channels.slack import SlackBot with responses.RequestsMock() as rsps: rsps.add( responses.POST, "https://slack.com/api/chat.postMessage", json={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General") await bot.send_text_message("ID", "my message") assert len(rsps.calls) == 1 last_call = rsps.calls[-1] request_params = urllib.parse.parse_qs(last_call.request.body) assert request_params == { "as_user": ["True"], "channel": ["General"], "text": ["my message"], "type": ["mrkdwn"], }
async def test_slackbot_send_image_url(): from rasa.core.channels.slack import SlackBot with responses.RequestsMock() as rsps: rsps.add( responses.POST, "https://slack.com/api/chat.postMessage", json={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General") url = "http://www.rasa.net" await bot.send_image_url("ID", url) assert len(rsps.calls) == 1 last_call = rsps.calls[-1] request_params = urllib.parse.parse_qs(last_call.request.body) assert request_params["as_user"] == ["True"] assert request_params["channel"] == ["General"] assert len(request_params["blocks"]) == 1 assert '"type": "image"' in request_params["blocks"][0] assert '"alt_text": "http://www.rasa.net"' in request_params["blocks"][ 0] assert '"image_url": "http://www.rasa.net"' in request_params[ "blocks"][0]
async def test_slackbot_send_image_url_threaded(): from rasa.core.channels.slack import SlackBot with aioresponses() as mocked: mocked.post( "https://www.slack.com/api/chat.postMessage", payload={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General", thread_id="DummyThread") url = "http://www.rasa.net" await bot.send_image_url("ID", url) r = latest_request(mocked, "POST", "https://www.slack.com/api/chat.postMessage") assert r request_params = json_of_latest_request(r) assert request_params["as_user"] is True assert request_params["channel"] == "General" assert request_params["thread_ts"] == "DummyThread" assert len(request_params["blocks"]) == 1 assert request_params["blocks"][0].get("type") == "image" assert request_params["blocks"][0].get( "alt_text") == "http://www.rasa.net" assert request_params["blocks"][0].get( "image_url") == "http://www.rasa.net"
async def test_slackbot_send_attachment_with_text(): from rasa.core.channels.slack import SlackBot with aioresponses() as mocked: mocked.post( "https://www.slack.com/api/chat.postMessage", payload={"ok": True, "purpose": "Testing bots"}, ) bot = SlackBot("DummyToken", "General") attachment = { "fallback": "Financial Advisor Summary", "color": "#36a64f", "author_name": "ABE", "title": "Financial Advisor Summary", "title_link": "http://tenfactorialrocks.com", "text": "Here is the summary:", "image_url": "https://r.com/cancel/r12", "thumb_url": "https://r.com/cancel/r12", "actions": [ { "type": "button", "text": "\ud83d\udcc8 Dashboard", "url": "https://r.com/cancel/r12", "style": "primary", }, { "type": "button", "text": "\ud83d\udccb XL", "url": "https://r.com/cancel/r12", "style": "danger", }, { "type": "button", "text": "\ud83d\udce7 E-Mail", "url": "https://r.com/cancel/r123", "style": "danger", }, ], "footer": "Powered by 1010rocks", "ts": 1531889719, } await bot.send_attachment("ID", attachment) r = latest_request(mocked, "POST", "https://www.slack.com/api/chat.postMessage") assert r request_params = json_of_latest_request(r) assert request_params == { "channel": "General", "as_user": True, "attachments": [attachment], }
async def test_action_utter_template_channel_specific(default_nlg, default_tracker, default_domain): from rasa.core.channels.slack import SlackBot output_channel = SlackBot("DummyToken", "General") events = await ActionUtterTemplate("utter_channel").run( output_channel, default_nlg, default_tracker, default_domain) assert events == [BotUttered("you're talking to me on slack!")]
async def test_slackbot_send_text_with_buttons_threaded(): from rasa.core.channels.slack import SlackBot with aioresponses() as mocked: mocked.post( "https://www.slack.com/api/chat.postMessage", payload={ "ok": True, "purpose": "Testing bots" }, ) bot = SlackBot("DummyToken", "General", thread_id="DummyThread") buttons = [{"title": "title", "payload": "payload"}] await bot.send_text_with_buttons("ID", "my message", buttons) r = latest_request(mocked, "POST", "https://www.slack.com/api/chat.postMessage") assert r request_params = json_of_latest_request(r) text_block = { "type": "section", "text": { "type": "plain_text", "text": "my message" }, } button_block = { "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "text": "title" }, "value": "payload", }], } assert request_params == { "as_user": True, "channel": "General", "text": "my message", "blocks": [text_block, button_block], "thread_ts": "DummyThread", }
async def test_response_channel_specific(default_nlg, default_tracker, default_domain): from rasa.core.channels.slack import SlackBot output_channel = SlackBot("DummyToken", "General") events = await ActionBotResponse("utter_channel").run( output_channel, default_nlg, default_tracker, default_domain ) assert events == [ BotUttered( "you're talking to me on slack!", metadata={"channel": "slack", "utter_action": "utter_channel"}, ) ]
async def test_slackbot_send_text(): from rasa.core.channels.slack import SlackBot responses.add( responses.POST, "https://slack.com/api/chat.postMessage", body='{"ok":true,"purpose":"Testing bots"}', ) bot = SlackBot("DummyToken", "General") await bot.send_text_message("ID", "my message") r = responses.calls[-1] assert r.request.body["as_user"] == ["True"] assert r.request.body["channel"] == ["General"] assert len(r.request.body["blocks"]) == 1 assert '"type": "section"' in r.request.body["blocks"][0] assert '"type": "plain_text"' in r.request.body["blocks"][0] assert '"text": "my message"' in r.request.body["blocks"][0]
async def test_slackbot_send_text(): from rasa.core.channels.slack import SlackBot responses.add( responses.POST, "https://slack.com/api/chat.postMessage", body='{"ok":true,"purpose":"Testing bots"}', ) bot = SlackBot("DummyToken", "General") await bot.send_text_message("ID", "my message") r = responses.calls[-1] assert r.parsed_body == { "as_user": ["True"], "channel": ["General"], "text": ["my message"], "type": ["mrkdwn"], }
async def test_slackbot_send_text(): from rasa.core.channels.slack import SlackBot httpretty.register_uri(httpretty.POST, 'https://slack.com/api/chat.postMessage', body='{"ok":true,"purpose":"Testing bots"}') httpretty.enable() bot = SlackBot("DummyToken", "General") await bot.send_text_message("ID", "my message") httpretty.disable() r = httpretty.latest_requests[-1] assert r.parsed_body == { 'as_user': ['True'], 'channel': ['General'], 'text': ['my message'] }
async def test_slackbot_send_image_url(): from rasa.core.channels.slack import SlackBot responses.add( responses.POST, "https://slack.com/api/chat.postMessage", body='{"ok":true,"purpose":"Testing bots"}', ) bot = SlackBot("DummyToken", "General") url = "http://www.rasa.net" await bot.send_image_url("ID", url) r = responses.calls[-1] assert r.request.body["as_user"] == ["True"] assert r.request.body["channel"] == ["General"] assert len(r.request.body["blocks"]) == 1 assert '"type": "image"' in r.request.body["blocks"][0] assert '"alt_text": "http://www.rasa.net"' in r.request.body["blocks"][0] assert '"image_url": "http://www.rasa.net"' in r.request.body["blocks"][0]
async def test_slackbot_send_image_url(): from rasa.core.channels.slack import SlackBot httpretty.register_uri(httpretty.POST, 'https://slack.com/api/chat.postMessage', body='{"ok":true,"purpose":"Testing bots"}') httpretty.enable() bot = SlackBot("DummyToken", "General") url = json.dumps([{"URL": "http://www.rasa.net"}]) await bot.send_image_url("ID", url) httpretty.disable() r = httpretty.latest_requests[-1] assert r.parsed_body['as_user'] == ['True'] assert r.parsed_body['channel'] == ['General'] assert len(r.parsed_body['attachments']) == 1 assert '"text": ""' in r.parsed_body['attachments'][0] assert '"image_url": "[{\\"URL\\": \\"http://www.rasa.net\\"}]"' \ in r.parsed_body['attachments'][0]
def test_slackbot_init_two_parameter(): from rasa.core.channels.slack import SlackBot bot = SlackBot("DummyToken", "General") assert bot.token == "DummyToken" assert bot.slack_channel == "General"
def test_slackbot_init_one_parameter(): from rasa.core.channels.slack import SlackBot ch = SlackBot("DummyToken") assert ch.token == "DummyToken" assert ch.slack_channel is None