https://aligenie.com/
登录,进入控制台,创建新技能。
点击后端服务,点击创建应用,关联阿里云账户,开通相关资源,创建技能应用;
语言选择python,模板选择地理小百科,点击创建,保存。
然后选择语言互交模型,点击左侧的实体,创建一个自定义实体
点击实体列表的编辑,添加实体值。
返回意图,点击图中的编辑,添加对话和参数。
前往后端服务,如图进入部署。
点击右下角的终端,输入pip install requests 安装模块
代码如下
import logging
import json
import base64
import random
import collections
import requests
USER_MAP = {}
KNOWLEDGE = [
"大米",
"餐巾纸",
"卫生纸",
"酱油"
]
KNOWLEDGEJJDid = [
"4592915",
"100015114144",
"1082266",
"100006624003"
]
def jdprice(l_index):
#京东
URL ='https://p.3.cn/prices/mgets?skuIds='+KNOWLEDGEJJDid[l_index]
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'Cookie': '',
'Connection': 'keep-alive',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Host': 'p.3.cn',
'Referer': 'https://book.jd.com/booktop/0-0-0.html?category=1713-0-0-0-10001-1'
}
res = requests.get(URL,headers=headers)
jdp=json.loads(res.text)
return jdp[0]['p']
KNOWLEDGEJTB = [
"55.3",
"60",
"56.9",
"100006624003"
]
def tbprice(l_index):
return KNOWLEDGEJTB[l_index]
KNOWLEDGEJPDD = [
"4592915",
"100015114144",
"1082266",
"100006624003"
]
def pddprice(l_index):
return KNOWLEDGEJPDD[l_index]
def pricecx(l_index,maket):
if maket=='京东':
return jdprice(l_index)
if maket=='淘宝':
return tbprice(l_index)
if maket=='拼多多':
return pddprice(l_index)
print(pricecx(0,'淘宝'))
def common_reply(reply, result_type):
response = {
"isBase64Encoded": "false",
"statusCode": "200",
"headers": {"content-type": "application/json"},
"body": {
"returnCode": "0",
"returnErrorSolution": "",
"returnMessage": "",
"returnValue": {
"reply": reply,
"resultType": result_type,
"executeCode": "SUCCESS",
"msgInfo": "",
}
}
}
return response
未指定追问参数,音箱自动开麦,用户的回答可跳转到其它意图
def ask_reply(reply):
return common_reply(reply, 'ASK_INF')
结束对话的回复,回复后音箱闭麦
def result_reply(reply):
return common_reply(reply, 'RESULT')
def handler(event, context):
request = json.loads(event)
logger = logging.getLogger()
body = base64.b64decode(request['body']).decode()
data = json.loads(body)
maket=data['slotEntities'][0]['originalValue']
logger.info('request body:' + body)
# 从请求中获取意图参数以及参数值
intent_name = data['intentName']
user_id = getattr(data['requestData'], 'userOpenId', 'testUser')
global USER_MAP
linked_list = USER_MAP.get(user_id)
# 欢迎意图 或 用户缓存数据为空
if intent_name == "welcome" or linked_list is None:
linked_list = collections.deque()
random_index = random.randint(0, len(KNOWLEDGE) - 1)
linked_list.append(random_index)
USER_MAP[user_id] = linked_list
return ask_reply(KNOWLEDGE[random_index]+'在'+maket+'平台的价格是'+pricecx(random_index,maket))
# 下一个意图,随机选择一个内容回复,并将index追加到用户数据的List集合最后
elif intent_name == "next":
random_index = random.randint(0, len(KNOWLEDGE) - 1)
linked_list.append(random_index)
USER_MAP[user_id] = linked_list
return ask_reply(KNOWLEDGE[random_index]+'在'+maket+'平台的价格是'+pricecx(random_index,maket))
# 上一个意图,将用户数据的List集合中最后一个index移除,并返回该index的内容。首先要判断集合内是否有元素,没有则不需要移除
elif intent_name == "prev":
if len(linked_list) > 0:
linked_list.pop()
if len(linked_list) == 0:
return ask_reply("这已经是第一个了。")
return ask_reply(KNOWLEDGE[linked_list[len(linked_list) - 1]] +'在'+maket+'平台的价格是'+pricecx(linked_list[len(linked_list) - 1],maket))
# 退出意图,清除用户缓存
elif intent_name == "exit":
USER_MAP.pop(user_id)
return result_reply("已为您退出,再见。")
return result_reply("请检查意图名称是否正确,或者新增的意图没有在代码里添加对应的处理分支。")