Example #1
0
    def test_ask_question_post_with_authorization(self):
        client = TestClient()
        client.add_license_keys_store()
        self._client_context = client.create_client_context("testid")

        self._client_context.brain._openchatbots._openchatbots['CHATBOT1'] = OpenChatBot("openchat1",
                                                                                         "http://localhost:5959/api/rest/v2.0/ask",
                                                                                         ["POST"],
                                                                                         "Basic",
                                                                                         "1234567890")

        mock_data = {"response": {
                            "text": "Hi there from chatbot1",
                        },
                        "status": {"code": 200, "text": "success"}
                     }
        mock_response = json.dumps(mock_data)

        service_config = BrainServiceConfiguration("openchatbot")
        service_config._method = 'POST'

        service = OpenChatRESTService(service_config, api=MockRestAPI(200, mock_response))

        response = service.ask_question(self._client_context, "chatbot1 Hello")
        self.assertIsNotNone(response)
        self.assertEqual("Hi there from chatbot1", response)
Example #2
0
    def test_ask_question_get(self):
        client = TestClient()
        client.add_license_keys_store()
        self._client_context = client.create_client_context("testid")

        self._client_context.brain._openchatbots._openchatbots[
            'CHATBOT1'] = OpenChatBot(
                "openchat1", "http://localhost:5959/api/rest/v2.0/ask", "GET")

        mock_data = {
            "response": {
                "text": "Hi there from chatbot1",
            },
            "status": {
                "code": 200,
                "text": "success"
            }
        }
        mock_response = json.dumps(mock_data)

        service = OpenChatMetaBotRESTService(
            BrainServiceConfiguration("openchatbot"),
            api=MockRestAPI(200, mock_response))

        response = service.ask_question(self._client_context, "chatbot1 Hello")
        self.assertIsNotNone(response)
        self.assertEquals(
            '{"response": {"text": "Hi there from chatbot1"}, "status": {"code": 200, "text": "success"}}',
            response)
Example #3
0
    def _try_connection(self, domain, protocol, ext):
        url = self._create_query_url(domain, protocol, ext)

        YLogger.debug(None, "Trying [%s]", url)
        reply = self._http_requests.get(url)

        if reply.status_code == 200:
            domaincb = OpenChatBot.create(domain, reply.json())
            self.cache_domain(domain, domaincb)
            return domaincb

        return None
Example #4
0
    def load_from_configuration(self,
                                configuration: BrainOpenChatBotsConfiguration):

        names = configuration.openchatbots()
        for name in names:
            openchatbot_config = configuration.openchatbot(name)
            self._openchatbots[name.upper()] = OpenChatBot(
                openchatbot_config.section_name, openchatbot_config.url,
                openchatbot_config.method, openchatbot_config.authorization,
                openchatbot_config.api_key)

        self._domain_handler = OpenChatBotDomainHandler(configuration)

        return True
    def test_construction_with_valid_json_no_port(self):
        payload = json.loads("""{
                       "openchatbot": {
                           "endpoint": "/api/mybot/v1.0/ask",
                           "host": "https://website1.com",
                           "methods": ["GET", "POST"]
                       }
                    }""")
        domaincb = OpenChatBot.create("openchatbot", payload)
        self.assertIsNotNone(domaincb)

        self.assertEqual(["GET", "POST"], domaincb.methods)

        self.assertEqual("https://website1.com/api/mybot/v1.0/ask",
                         domaincb.url)