当前位置:网站首页>微信公告号自动回复使用图灵机器人实现智能回复
微信公告号自动回复使用图灵机器人实现智能回复
2022-06-29 15:29:00 【种豆走天下】
微信公告号自动回复使用图灵机器人实现智能回复
一言堂接口调用,自动回复一句话,接上一节博客
HitokotoUtil .java:
package com.qfjy.project.weixin.api.hitokoto;
import com.qfjy.project.weixin.util.WeixinUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
/** * @Classname HitokotoUtil * @Author guoweixin * @Description TODO 一言 一句话服务 https://v1.hitokoto.cn/ * @Date 2021/6/24 14:15 * @Created by Administrator */
@Component
public class HitokotoUtil {
private static String HITOKOTO_URL="https://v1.hitokoto.cn/";
/** java jdk原生:UrlConnection apache:HttpClient OKHTTP spring框架:RestTemplate */
public String getResult(){
JSONObject jsonObject= WeixinUtil.httpRequest(HITOKOTO_URL,"GET",null);
String result=jsonObject.getString("hitokoto");
return result;
}
}
在CoreService.java中:
1.导入对象
@Autowired
private HitokotoUtil hitokotoUtil;
2.调用对象
// 文本消息
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
respContent = hitokotoUtil.getResult();
//respContent = "您发送的是文本消息!";
}
使用图灵机器人实现智能回复
图灵接口每天可使用100次:
InputText.java:
package com.qfjy.project.weixin.api.tuling.bean;
import lombok.Data;
@Data
public class InputText {
/**直接输入文本*/
private String text;
}
Perception.java:
package com.qfjy.project.weixin.api.tuling.bean;
import lombok.Data;
@Data
public class Perception {
/**文本消息*/
private InputText inputText;
}
TulingBean.java:
package com.qfjy.project.weixin.api.tuling.bean;
import lombok.Data;
@Data
public class TulingBean {
/** 输入类型:0-文本(默认)、1-图片、2-音频*/
private int reqType=0;
/**输入信息*/
private Perception perception;
/**用户参数*/
private UserInfo userInfo;
}
TulingUtil.java:
package com.qfjy.project.weixin.api.tuling;
import com.qfjy.project.weixin.api.tuling.bean.InputText;
import com.qfjy.project.weixin.api.tuling.bean.Perception;
import com.qfjy.project.weixin.api.tuling.bean.TulingBean;
import com.qfjy.project.weixin.api.tuling.bean.UserInfo;
import com.qfjy.project.weixin.util.WeixinUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
@Component
public class TulingUtil {
/** * 注册图灵平台 http://www.tuling123.com/ * 并完成实名认证。申请机器人。 * API教程接入文档:https://www.kancloud.cn/turing/www-tuling123-com/718227 */
private static String TULING_URL_POST="http://openapi.tuling123.com/openapi/api/v2";
public String getResult(String msg){
//1对象 转 JSON
TulingBean tulingBean=new TulingBean();
tulingBean.setReqType(0);
Perception perception=new Perception();
InputText inputText=new InputText();
inputText.setText(msg);
perception.setInputText(inputText);
tulingBean.setPerception(perception);
UserInfo userInfo=new UserInfo();
userInfo.setUserId("java2101");
userInfo.setApiKey("5d46f432d7a744dab2d0cdb7f5e5532b");
tulingBean.setUserInfo(userInfo);
JSONObject json1=JSONObject.fromObject(tulingBean);
System.out.println(json1.toString());
//2 向服务器接口地址 发送POST请求
JSONObject jsonObject= WeixinUtil.httpRequest(TULING_URL_POST,"POST",json1.toString());
//3得到图灵服务器机器人返回的结果
JSONArray json2= (JSONArray) jsonObject.get("results");
JSONObject json3= (JSONObject) json2.get(0);
JSONObject json4= (JSONObject) json3.get("values");
String result= json4.getString("text");
return result;
}
public static void main(String[] args) {
//1对象 转 JSON
TulingBean tulingBean=new TulingBean();
tulingBean.setReqType(0);
Perception perception=new Perception();
InputText inputText=new InputText();
inputText.setText("吃饭了吗");
perception.setInputText(inputText);
tulingBean.setPerception(perception);
UserInfo userInfo=new UserInfo();
userInfo.setUserId("java2101");
userInfo.setApiKey("acc513be8b5e4b26929247e83132f116");
tulingBean.setUserInfo(userInfo);
JSONObject json1=JSONObject.fromObject(tulingBean);
System.out.println(json1.toString());
//2 向服务器接口地址 发送POST请求
JSONObject jsonObject= WeixinUtil.httpRequest(TULING_URL_POST,"POST",json1.toString());
//3得到图灵服务器机器人返回的结果
JSONArray json2= (JSONArray) jsonObject.get("results");
JSONObject json3= (JSONObject) json2.get(0);
JSONObject json4= (JSONObject) json3.get("values");
String result= json4.getString("text");
System.out.println(result);
}
}
在CoreService.java中:
1.导入对象
@Autowired
private TulingUtil tuLingUtil;
2.调用对象
// 文本消息
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
respContent = tuLingUtil.getResult(content);
//respContent = hitokotoUtil.getResult();
//respContent = "您发送的是文本消息!";
}
下一节,使用五个图灵机器人实现循环,若第一个机器人回复次数使用完,则可以使用下一个机器人,五次机会使用完,则返回提示。
边栏推荐
猜你喜欢

I am 35 years old. Can I change my career to be a programmer?

14.IP协议-bite
![中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]](/img/c6/a2780620fef8bc5f7b564ef173f589.png)
中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]

LeetCode-64-最小路径和

EasyGBS调用获取实时快照接口时,出现白色方块该如何解决?

12.udp protocol -bite

Sofaregistry source code | data synchronization module analysis

Chapter IX app project test (the end of this chapter)

Create an API rapid development platform, awesome!

Unity C basic review 27 - delegation example (p448)
随机推荐
What is the time complexity of the redis command?? (the actual question is about the underlying structure of redis)
cmake学习-2
Informatics Olympiad all in one 2061: trapezoidal area
Unity C # basic review 26 - first acquaintance Commission (p447)
Middle order and post order traversal to construct binary tree [recursive partition interval and backtracking splicing subtree + similarity and difference between middle post order and middle pre orde
ImgUtil 图片处理工具类,文字提取,图片水印
Pytorch two-dimensional multi-channel convolution operation method
Flink SQL task taskmanager memory settings
curl: (56) Recv failure: Connection reset by peer
关于SQL+NoSQL : NewSQL数据库
Leetcode notes: biweekly contest 81
Excel中构建SQL语句
极化SAR几种成像模式
Mingdeyang xilinx-k7-325t/410t core board data manual
Google software version experience cycle
JD health responded that it planned to acquire JD assets with us $355.4 million: related to pet health product category
Training mode of deep learning network
About sql+nosql: newsql database
卷积神经网络中各层的作用
LeetCode笔记:Biweekly Contest 81