? 介绍
最终效果可以观看视频:https://h5.pipix.com/s/NTHB9gk/
? 项目结构
? 相关技术springboot
使用框架springboot,快速搭建后端服务。
###? 搭建教程
打开天猫精灵技能官网:https://iap.aligenie.com/
然后选择私域技能:
语音交互模型,虽然我们无调用词,因为无调用词,需要获取你的设备openid,后面会介绍到。
点击创建一个意图。
这里一定要设置默认意图。一会调用就可以获取到我们无调用词的相关数据了。
服务部署。
其实这里就是验证这个服务器是你的。
下载认证数据。
这就是添加好了的。
如果要使用推送功能必须要申请这个权限。
下面就是申请推送语音播报的模板了。
点击新建,可以根据个人需要进行申请。
下面就是测试了。我们开始搭建服务端。
其实这个就回到了最初的哪个问题,我们是无调用词的,怎么触发服务端的代码呢?如何获取一些我们需要的东西呢?
我们在编辑界面,设置一下调用词
然后写好代码,然后上传服务器。
@RequestMapping("/welcome")
public ResultModel<TaskResult> taskResult(@RequestBody String json){
// ResultModel<TaskResult> res = new ResultModel<>();
log.info("json:{}",json);
TaskQuery taskQuery =MetaFormat.parseToQuery(json);
TaskResult taskResult = new TaskResult();
// 从请求中获取意图参数以及参数值
Map<String, String> paramMap = taskQuery.getSlotEntities().stream().collect(
Collectors.toMap(slotItem -> slotItem.getIntentParameterName(),
slotItem -> slotItem.getOriginalValue()));
//处理名称为 welcome 的意图
if ("welcome".equals(taskQuery.getIntentName())) {
taskResult.setReply("欢迎使用自定义技能~");
log.info("json:{}",json);
//处理名称为 weather 的意图
}else {
taskResult.setReply("请检查意图名称是否正确,或者新增的意图没有在代码里添加对应的处理分支。");
}
return reply(taskResult);
}
因为我们需要相关id,我们需要在测试界面,进行真机测试
实际调用,可以看到我们需要的数据,我们可以用到。
?实现自动推送
打开自动推送:
https://open-api.aligenie.com/?version=iap_1.0&apiName=PushNotifications
package com.ylesb.demo.controller;
/**
* @title: PushNotificationsShowHttpDemo
* @projectName demo
* @description: TODO
* @author White
* @site : [www.ylesb.com]
* @date 2022/4/914:08
*/
import com.ylesb.demo.utils.TeaRequest;
import com.ylesb.demo.utils.Client;
import com.ylesb.demo.utils.TimeUtil;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @className : PushNotificationsShowHttpDemo
* @description : [不使用sdk,直接发送http请求,存在bug]
* @author : [XuGuangchao]
* @site : [www.ylesb.com]
* @version : [v1.0]
* @createTime : [2022/4/9 14:08]
* @updateUser : [XuGuangchao]
* @updateTime : [2022/4/9 14:08]
* @updateRemark : [描述说明本次修改内容]
*/
public class PushNotificationsShowHttpDemo {
private static String ACCESS_KEY = "应用的accessKey";
private static String ACCESS_SECRET = "应用的accessSecret";
private static String HTTP_URL = "http://openapi.aligenie.com/v1.0/iap/notifications";
public static void main(String[] args) {
System.out.println(getISO8601Timestamp(new Date()) + " UTC");
Map<String, String> query = new HashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put("x-acs-date", TimeUtil.getUtcTime());
headers.put("x-acs-action", "PushNotifications");
headers.put("x-acs-version", "iap_1.0");
headers.put("Content-Type", "application/json");
headers.put("x-acs-signature-nonce", UUID.randomUUID().toString());
TeaRequest req = new TeaRequest();
req.query = query;
req.headers = headers;
req.method = "PUT";
req.pathname = "/v1.0/iap/notifications";
try {
//模板占位符填充信息
Map<String, Object> placeHolder = new HashMap<>();
placeHolder.put("ip", "192.168.0.1");
Map<String, Object> sendTarget = new HashMap<>();
sendTarget.put("TargetIdentity", "useropenid");
//你使用什么就获取写什么
sendTarget.put("TargetType", "USER_OPEN_ID");
Map<String, Object> notificationUnicastRequest = new HashMap<>();
notificationUnicastRequest.put("PlaceHolder", placeHolder);
notificationUnicastRequest.put("MessageTemplateId", "7hYJkLiqM381wUQr");
notificationUnicastRequest.put("EncodeType", "SKILL_ID");
notificationUnicastRequest.put("SendTarget", sendTarget);
notificationUnicastRequest.put("EncodeKey", "88074");
notificationUnicastRequest.put("OrganizationId","1325195918262396007");
// 是否是调试
notificationUnicastRequest.put("IsDebug",true);
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("NotificationUnicastRequest", notificationUnicastRequest);
bodyMap.put("TenantInfo", new HashMap<>());
Gson gson = new Gson();
String body = gson.toJson(bodyMap);
System.out.println(body);
String authorization = Client.getAuthorization(req,
body, ACCESS_KEY, ACCESS_SECRET);
headers.put("Authorization", authorization);
HttpPut httpPut = new HttpPut(HTTP_URL);
if (StringUtils.isNotEmpty(body)) {
httpPut.setEntity(new StringEntity(body, Consts.UTF_8));
}
if (!headers.isEmpty()) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPut.addHeader(entry.getKey(), entry.getValue());
}
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(2000)
.setSocketTimeout(2000).build();
CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setMaxConnTotal(200)
.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
.setDefaultRequestConfig(requestConfig)
.build();
HttpResponse response = httpClient.execute(httpPut);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity, Consts.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 传入Data类型日期,返回字符串类型时间(ISO8601标准时间)
* @param date
* @return
*/
public static String getISO8601Timestamp(Date date){
TimeZone tz = TimeZone.getTimeZone("GMT");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(date);
return nowAsISO;
}
// 根据Date时间生成UTC时间函数
public static String generateUTCTime(Date time) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
dateFormat.setLenient(false);
return dateFormat.format(time);
}
}
可以看到我们需要这些参数,最重要的是刚才我们在服务端获取的。
DEVICE_UNION_ID :设备unionId
DEVICE_OPEN_ID :设备openId
USER_UNION_ID :用户unionId
USER_OPEN_ID :用户openId
这些值。我们需要用到的。剩下的跟着官方教程就可以完成了。
最后附上项目代码地址:
https://github.com/CoderXGC/aligenie-demo
https://gitee.com/CoderXGC/aligenie-demo