代码拉取完成,页面将自动刷新
# -*- coding: utf-8-*-
import json
import requests
from robot import config, logging
from robot.sdk.AbstractPlugin import AbstractPlugin
logger = logging.getLogger(__name__)
class Plugin(AbstractPlugin):
SLUG = "roadcondition"
def request(self, url, params):
result = requests.post(url, data=params)
return json.loads(result.text, encoding='utf-8')
def onAsk(self, input, app_key, adcode):
if input is None:
input = "龙岗大道"
url_transit = "http://restapi.amap.com/v3/traffic/status/road"
params = {"adcode" : adcode,"name" : input,"key" : app_key}
res = self.request(url_transit,params)
logger.debug(res)
if res:
status = res["status"]
if status == "1":
logger.debug("status == 1")
logger.debug(res['trafficinfo'])
if len(res['trafficinfo']) > 0:
trafficinfo = res['trafficinfo']['evaluation']['description']
trafficinfo1 = res['trafficinfo']['description']
self.say(trafficinfo)
self.say(trafficinfo1)
else:
self.say(u"无法获取到信息")
return
else:
logger.error(u"接口错误:")
return
else:
logger.error(u"接口调用失败")
return
def handle(self, text, parsed):
profile = config.get()
if self.SLUG not in profile or \
'app_key' not in profile[self.SLUG] or \
'adcode' not in profile[self.SLUG]:
self.say(u"插件配置有误,插件使用失败")
return
app_key = profile[self.SLUG]['app_key']
adcode = profile[self.SLUG]['adcode']
self.say(u'哪条道路?', onCompleted=lambda: self.onAsk(self.activeListen(), app_key, adcode))
def isValid(self, text, parsed):
return any(word in text for word in [u"路况"])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。