Rasa Core version:
0.12.2
Python version:
3.6
Operating system (windows, osx, …):
Windows 10
Issue:
We have trained our model and the bot is giving proper output in the console. Now we have tried integrating with slack using python.
We have created 2 python script.
First we run rasa_slack_connector.py
The code is attached below :
from future import absolute_import
from future import division
from future import print_function
from future import unicode_literals
import logging
from builtins import str
from flask import Blueprint, request, jsonify
from rasa_core.channels.channel import UserMessage, OutputChannel, InputChannel
#from rasa_core.channels.rest import HttpInputComponent
logger = logging.getLogger(name)
class SlackBot(OutputChannel):
def init(self, slack_verification_token, channel):
self.slack_verification_token = slack_verification_token
self.channel = channel
def send_text_message(self, recipient_id, message):
from slackclient import SlackClient
text = message
recipient = recipient_id
CLIENT = SlackClient(self.slack_verification_token)
CLIENT.api_call('chat.postMessage', channel = self.channel, text = text, as_user = True)
class SlackInput(InputChannel):
def init(self, slack_dev_token, slack_verification_token, slack_client, debug_mode):
self.slack_dev_token = slack_dev_token
self.debug_mode = debug_mode
self.slack_client = slack_client
self.slack_verification_token = slack_verification_token
def blueprint(self, on_new_message):
from flask import Flask, request, Response
slack_webhook = Blueprint('slack_webhook', __name__)
@slack_webhook.route('/', methods = ['GET'])
def health():
return jsonify({'status':'ok'})
@slack_webhook.route('/slack/events', methods = ['POST'])
def event():
if request.json.get('type') == 'url_verification':
return request.json.get('challenge'), 200
if request.json.get('token') == self.slack_client and request.json.get('type') == 'event_callback':
data = request.json
messaging_events = data.get('event')
channel = messaging_events.get('channel')
user = messaging_events.get('user')
text = messaging_events.get('text')
bot = messaging_events.get('bot_id')
if bot == None:
on_new_message(UserMessage(text, SlackBot(self.slack_verification_token, channel)))
return Response(), 200
return slack_webhook
run_app.py
from rasa_core.channels.slack import SlackInput
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
import yaml
from rasa_core.utils import EndpointConfig
from rasa_core.channels.channel import InputChannel
nlu_interpreter = RasaNLUInterpreter(‘./models/default/shoenlu’)
action_endpoint = EndpointConfig(url=“http://localhost:5055/webhook”)
agent = Agent.load(‘./models/dialogue’, interpreter = nlu_interpreter, action_endpoint = action_endpoint)
input_channel = SlackInput(‘', #app verification token
'’, # bot verification token
‘*******************’) # slack verification token
agent.handle_channels([input_channel], 5004, serve_forever=True)
We run the run_app.py and get the following in the console :
We parallelly run ngrok using the following command :
ngrok http 5004
We use the forwarding port from it and use it in slack Event Subscription.
When we send ‘hi’ to the bot using slack we get 200 status for POST but the bot does not reply back.
Content of domain file (if used & relevant):
``