from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging

import requests
from flask import Blueprint, request, jsonify, abort
from flask_cors import CORS, cross_origin

from rasa_core.channels.channel import UserMessage, OutputChannel
from rasa_core.channels.rest import HttpInputComponent

logger = logging.getLogger(__name__)


class NoOutputChannel(OutputChannel):
    """Output channel that does nothing. Really, just nothing."""

    def send_text_message(self, recipient_id, message):
        # type: (Text, Text) -> None
        """Just ignore everything."""
        pass


class RasaChatInput(HttpInputComponent):
    def __init__(self, host):
        self.host = host

    def blueprint(self, on_new_message):
        rasa_chat = Blueprint('rasa_chat', __name__)
        CORS(rasa_chat)

        @rasa_chat.route("/", methods=['GET'])
        def health():
            return jsonify({"status": "ok"})

        @rasa_chat.route("/send", methods=['GET', 'POST'])
        @cross_origin()
        def receive():
            user = fetch_user(self.host, request)
            msg = request.json["message"]
            if user.get("role") == "admin":
                conversation_id = request.args.get("conversation",
                                                   user["username"])
            else:
                conversation_id = user["username"]
            on_new_message(UserMessage(msg, NoOutputChannel(),
                                       sender_id=conversation_id))

            return jsonify({"status": "ok"})

        return rasa_chat


def check_token(host, token):
    url = "{}/users/me".format(host)
    headers = {"Authorization": token}
    result = requests.get(url, headers=headers)

    if result.status_code == 200:
        return result.json()
    else:
        logger.info("Failed to check token: {}. "
                    "Content: {}".format(token, request.data))
        return None


def fetch_user(host, req):
    user = None
    if req.headers.get("Authorization"):
        user = check_token(host, req.headers.get("Authorization"))
    if user:
        return user

    user = check_token(host, req.args.get('token', default=None))
    if user:
        return user
    abort(401)

@@ -668,16 +668,17 @@
 ing.%22%22%22%0A
+%0A
         
@@ -721,24 +721,72 @@
 Component):%0A
+    %22%22%22Chat input channel for Rasa Platform%22%22%22%0A%0A
     def __in
@@ -829,16 +829,811 @@
 = host%0A%0A
+    def _check_token(self, token):%0A        url = %22%7B%7D/users/me%22.format(self.host)%0A        headers = %7B%22Authorization%22: token%7D%0A        result = requests.get(url, headers=headers)%0A%0A        if result.status_code == 200:%0A            return result.json()%0A        else:%0A            logger.info(%22Failed to check token: %7B%7D. %22%0A                        %22Content: %7B%7D%22.format(token, request.data))%0A            return None%0A%0A    def fetch_user(self, req):%0A        %22%22%22Fetch user from the Rasa Platform Admin API%22%22%22%0A%0A        if req.headers.get(%22Authorization%22):%0A            user = self._check_token(req.headers.get(%22Authorization%22))%0A            if user:%0A                return user%0A%0A        user = self._check_token(req.args.get('token', default=None))%0A        if user:%0A            return user%0A        abort(401)%0A%0A
     def 
@@ -1983,16 +1983,21 @@
  user = 
+self.
 fetch_us
@@ -2003,19 +2003,8 @@
 ser(
-self.host, 
 requ
@@ -2468,32 +2468,32 @@
 tatus%22: %22ok%22%7D)%0A%0A
+
         return r
@@ -2505,668 +2505,4 @@
 hat%0A
-%0A%0Adef check_token(host, token):%0A    url = %22%7B%7D/users/me%22.format(host)%0A    headers = %7B%22Authorization%22: token%7D%0A    result = requests.get(url, headers=headers)%0A%0A    if result.status_code == 200:%0A        return result.json()%0A    else:%0A        logger.info(%22Failed to check token: %7B%7D. %22%0A                    %22Content: %7B%7D%22.format(token, request.data))%0A        return None%0A%0A%0Adef fetch_user(host, req):%0A    user = None%0A    if req.headers.get(%22Authorization%22):%0A        user = check_token(host, req.headers.get(%22Authorization%22))%0A    if user:%0A        return user%0A%0A    user = check_token(host, req.args.get('token', default=None))%0A    if user:%0A        return user%0A    abort(401)%0A

add comments and restructure