from xiaoai import *
import random
def outputJson(toSpeakText, is_session_end, attribute=None, openMic=True):
xiaoAIResponse=XiaoAIResponse(to_speak=XiaoAIToSpeak(type_=0, text=toSpeakText), open_mic=openMic)
response = xiaoai_response(XiaoAIOpenResponse(version="1.0",is_session_end=is_session_end,response=xiaoAIResponse,session_attributes=attribute))
return response
def main(event):
req = xiaoai_request(event)
sum_person = 0
selected_num = 0
rand =0
count = 0
dictionary = {}
attribute = {"sum_person":0}
if req.request.type == 0:
attribute['sum_person'] = 200
attribute['dictionary'] = dictionary
attribute["selected_num"] = 0
return outputJson("欢迎进入抽签小工具,请先向小爱说明抽签人数,如 一共10个人", False, attribute)
elif req.request.type == 1:
if ((not hasattr(req.request, "slot_info")) or (not hasattr(req.request.slot_info, "intent_name"))):
return outputJson("抱歉,我还没有听懂。请先说明抽签人数或者直接对小爱说抽签。", False, None)
else:
if req.request.slot_info.intent_name == 'random_sum':
slotsList = req.request.slot_info.slots
sum_person = [item for item in slotsList if item['name'] == 'random_sum'][0]['value']
if int(sum_person) > 200:
sum_person = 200
elif int(sum_person) < 1:
sum_person = 1
attribute["sum_person"] = int(sum_person)
attribute["selected_num"] = int(selected_num)
attribute['dictionary'] = dictionary
return outputJson("已设置参与人数" + str(sum_person) + "人,请大家依次对小爱说 抽签 抽取自己的签号", False,attribute)
elif req.request.slot_info.intent_name == 'random_select':
if hasattr(req.session, "attributes"):
sum_person = int(req.session.attributes["sum_person"])
selected_num = int(req.session.attributes["selected_num"])
dictionary = req.session.attributes["dictionary"]
attribute["sum_person"] = int(sum_person)
if selected_num == sum_person:
attribute['dictionary'] = dictionary
attribute["sum_person"] = selected_num
attribute["selected_num"] = selected_num
return outputJson("签号都被抽完了,您可以向小爱说 所有人 来查看抽签信息或者重新设置人数哦", False,attribute)
else:
rand = random.randint(1,sum_person)
print("dictionary: "+ str(dictionary))
dictionary.setdefault(str(rand),1)
attribute['dictionary'] = dictionary
attribute["selected_num"] = int(selected_num+1)
return outputJson("签号:"+str(rand)+",还剩"+str(sum_person-selected_num-1)+"个签位", False, attribute)
elif req.request.slot_info.intent_name == 'all_selected_info':
if hasattr(req.session, "attributes"):
sum_person = int(req.session.attributes["sum_person"])
selected_num = int(req.session.attributes["selected_num"])
dictionary = req.session.attributes["dictionary"]
attribute = req.session.attributes
str_output = "已经选择的签号依次为:"
j = 1
for i in dictionary.keys():
str_output += str(j)+"号签:"+ str(i) +", "
j+=1
str_output +="还剩"+ str(sum_person-selected_num)+"个签位未选择。"
print(str_output)
return outputJson(str_output, False, attribute)
else:
return outputJson("抱歉,我没有听懂呢", False,None)
else:
return outputJson("感谢使用抽签小工具,下次再见哦~", True, None, False)