我的聊天机器人无法访问API

时间:2018-06-29 12:49:13

标签: python chatbot

我一直困扰我的Weather bot问题。除了指定位置后进行天气预报的动作(实际上是主要动作)外,对话正常进行。我正在使用Apixu进行天气预报

当我运行在线培训时,出现此错误:

ERROR:rasa_core.processor:Encountered an exception while running action 'action_weather'. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code.

这是我用于天气预报的python脚本:

from __future__ import absolute_import from __future__ import division
__future__ import unicode_literals


from rasa_core.actions.action import Action from rasa_core.events
import SlotSet from apixu.client import ApixuClient

class ActionWeather(Action):
     def name(self):
         return 'action_weather'

     def run(self, dispatcher, tracker, domain):

         api_key = '6******************'
         client = ApixuClient(api_key)

         loc = tracker.get_slot('location')
         current = client.getCurrentWeather(q=loc)

         country = current['location']['country']
         city = current['location']['name']
         condition = current['current']['condition']['text']
         temperature_c = current['current']['temp_c']
         humidity = current['current']['humidity']
         wind_mph = current['current']['wind_mph']

         response = """It is currently {} in {} at the moment. The temperature is {} degrees, the humidity is {}%
          and the wind speed is {} mph.""".format(condition, city, temperature_c, humidity, wind_mph)

         dispatcher.utter_message(response)
         return [SlotSet('location', loc)]

这是我的天气域文件,是Yaml文件

 slots:   location:
     type: text


 intents:
  - greet
  - goodbye
  - inform


 entities:
  - location

 templates:   utter_greet:
     - 'Hello! How can I help?'   utter_goodbye:
     - 'Talk to you later.'
     - 'Bye bye :('   utter_ask_location:
     - 'In what location?'



 actions:
  - utter_greet
  - utter_goodbye
  - utter_ask_location
  - actions.ActionWeather

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

由于您的机器人使用的是Python,因此该程序使用ruamel.yaml或PyYAML来解析YAML文件。

如果您尝试解析YAML文件,则很明显在第一行它是无效的:

import ruamel.yaml
yaml = ruamel.yaml.YAML()

yaml_str = """\
 slots:   location:
     type: text
"""

data = yaml.load(yaml_str)

给予:

ruamel.yaml.scanner.ScannerError: mapping values are not allowed here
  in "<unicode string>", line 1, column 19:
     slots:   location:
                      ^ (line: 1)

您也可以尝试此online

您的机器人很可能捕获了这些错误,但未对它们采取适当的措施。