Does Blueprint objects have no root attributes?

1

I followed the climate chatbot rasa provided by Justina Petraityte, you can find the GitHub repository here . She tries to connect her chatbot to Slack. This implies the use of Flask to create the links. However, unless I'm wrong, it seems that your code is outdated: Does Blueprint objects have no root attributes? Because when I ran the script run_app.py I had this problem:

(MoodbotEnv) mike@mike-thinks:~/Programing/Rasa_tutorial/moodbot4$ python run_app.py 
...
Traceback (most recent call last):
  File "run_app.py", line 11, in <module>
    agent.handle_channel(HttpInputChannel(5004,'/',input_channel))
  File "/home/mike/Programing/Rasa_tutorial/moodbot4/MoodbotEnv/lib/python3.5/site-packages/rasa_core/agent.py", line 122, in handle_channel
    processor.handle_channel(input_channel)
  File "/home/mike/Programing/Rasa_tutorial/moodbot4/MoodbotEnv/lib/python3.5/site-packages/rasa_core/processor.py", line 60, in handle_channel
    input_channel.start_sync_listening(self.handle_message)
  File "/home/mike/Programing/Rasa_tutorial/moodbot4/MoodbotEnv/lib/python3.5/site-packages/rasa_core/channels/rest.py", line 36, in start_sync_listening
    self._record_messages(message_handler)
  File "/home/mike/Programing/Rasa_tutorial/moodbot4/MoodbotEnv/lib/python3.5/site-packages/rasa_core/channels/rest.py", line 52, in _record_messages
    app.register_blueprint(component.blueprint(on_message))
  File "/home/mike/Programing/Rasa_tutorial/moodbot4/rasa_slack_connector.py", line 42, in blueprint
    @slack_webhook.root('/', methods=['GET'])
AttributeError: 'Blueprint' object has no attribute 'root'

Here is the script in question:

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
from rasa_core.channels.rest import HttpInputComponent

logger = logging.getLogger(__name__)

# send messages back to slack
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
        recipient = recipient_id
        text= message

        CLIENT = SlackClient(self.slack_verification_token)
        CLIENT.api_call('chat.postMessage',channel = self.channel,text = text,as_user = True)


class SlackInput(HttpInputComponent):
    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__)

        # create the routes, trigger specific functions, using post requests, but if get, let's create something to deal with it
        @slack_webhook.root('/', methods=['GET'])
        def health():
            return jsonify({'status' : 'ok'})

        # We are going to get messages from slack using POST request. The first thing, it will create a challenge request
        # so we must have a check for this url 
        @slack_webhook.root('/slack/events', methods=['POST'])
        def event():
            if request.json.get('type') == 'url_verification':
                # response the same challenge message than the one that was sent to me
                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('user')
                user =  messaging_events.get('text')
                # not tot have the bot answering to himself
                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

However, it seems that you have updated the code recently. Hence my doubts about my error

    
asked by ThePassenger 22.05.2018 в 20:56
source

0 answers