标题: 写了个天猫精灵控制电脑关机的脚本
时间: 2023-04-26
import json
import os
import platform
from flask import Flask, request
app = Flask(__name__)
SHUTDOWN_COMMAND = "shutdown /s /t 0" # Windows关机命令
@app.route('/aligenie/你的鉴权文件.txt', methods=['GET'])
def get_file():
response = '鉴权文件内容
return response, 200, {'Content-type': 'text/plain'}
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
data = {'message': 'Hello, world!'}
json_data = json.dumps(data)
return json_data, 200, {'Content-type': 'application/json'}
elif request.method == 'POST':
data = request.get_json()
intent_name = data.get('intentName', '未知意图')
if intent_name == '关机':
print('开始关机')
# Windows关机命令
if platform.system() == "Windows":
os.system(SHUTDOWN_COMMAND)
else:
print("Unsupported OS")
# 发送回复消息
response_data = {
"returnCode": "0",
"returnErrorSolution": "",
"returnMessage": "",
"returnValue": {
"reply": "正在尝试关机",
"resultType": "RESULT",
"executeCode": "SUCCESS"
}
}
response_body = json.dumps(response_data).encode('utf-8')
return response_body, 200, {'Content-type': 'application/json'}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9096)
『回复列表(17|隐藏机器人聊天)』
@残缘,以下是将天猫精灵的技能接收格式改为小爱同学的修改后的代码:
import json
import os
import platform
from flask import Flask, request
app = Flask(__name__)
SHUTDOWN_COMMAND = "shutdown /s /t 0" # Windows关机命令
@app.route('/auth', methods=['POST'])
def get_auth():
data = {
"authType": "AUTH",
"data": {
"accessToken": "你的小爱同学accessToken",
"expireTime": 1234567890,
"userId": "小爱同学的用户ID",
"authMethods": ["VOICEPRINT", "FACEPRINT"],
"deviceList": [{
"deviceId": "小爱同学的设备ID",
"deviceName": "小爱同学的设备名称",
"deviceType": "DEVICE_TYPE",
"iconUrl": "设备图标的URL",
"properties": {
"bluetooth": true,
"wifi": true
},
"zone": "Asia/Shanghai"
}]
}
}
return json.dumps(data), 200, {'Content-type': 'application/json'}
@app.route('/query', methods=['POST'])
def query():
data = request.get_json()
intent_name = data.get('intent', {}).get('intentName', '未知意图')
if intent_name == '关机':
print('开始关机')
# Windows关机命令
if platform.system() == "Windows":
os.system(SHUTDOWN_COMMAND)
else:
print("Unsupported OS")
# 发送回复消息
response_data = {
"version": "1.0",
"response": {
"action": {
"say": "正在尝试关机",
},
"shouldEndSession": True
}
}
return json.dumps(response_data), 200, {'Content-type': 'application/json'}
else:
# 发送回复消息
response_data = {
"version": "1.0",
"response": {
"action": {
"say": "不知道你在说什么呢",
},
"shouldEndSession": True
}
}
return json.dumps(response_data), 200, {'Content-type': 'application/json'}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9096)
在这个修改后的代码中,我们新增了两个路由函数,一个用于获取小爱同学的鉴权信息,一个用于处理技能的查询请求。小爱同学的鉴权信息请求的URL为/auth
,返回的数据格式需要符合小爱同学的要求。查询请求的URL为/query
,查询请求的格式也需要符合小爱同学的要求。我们在查询请求中获取意图名称并执行相应的操作,然后返回符合小爱同学要求的响应数据。