微信测试号开发之六 图灵自动回复文本消息

 

介绍:使用图灵机器人来自动回复文本类的消息

 

(一):图灵官网注册、登录,然后获取key值(key是调用图灵回复接口的必须参数)

官网网址:http://www.tuling123.com/

登录后进入机器人设置,找到APIkey

 

 

(二):编写实现回复的代码

 

 

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mote.weixin.utils.CommonUtils;

public class TuLinService {
	/**
	 * 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果
	 * 
	 * @param content
	 * @return
	 */
        //对应刚才获取的APIkey
	private static final String KEY = "cc11ec7832854becabfd7a6fd7c271a6";

	private static final ObjectMapper MAPPER = new ObjectMapper();

	public static String tulinResult(String content) {

		String result = null;
		try {
			//图灵回复地址,content是用户发送的文本消息
			String apiUrl = "http://www.tuling123.com/openapi/api?key=" + KEY + "&info=" + content;
			
			//发送get请求,CommonUtils是前几篇中的内容
			String resq = CommonUtils.Get(apiUrl);
			
			//如果返回为空
			if (null == resq) {
				return "你的言语理解起来好难啊……";
			}

			//获取返回码
			String code = MAPPER.readTree(resq).get("code").toString();
			if (code.equals("100000")) {
				//获取回复内容
				result = MAPPER.readTree(resq).get("text").toString().replaceAll("\"", "");
			}

			return result;
			
		} catch (Exception e) {
			System.out.println("图灵自动回复失败");
			e.printStackTrace();
		}
		return result;

	}
}

 

 

(三):在文本消息处理中,调用图灵


在CoreService的文本消息处理中,调用图灵

 

// 文本消息
		if (msgType.equals(MessageUtils.REQ_MESSAGE_TYPE_TEXT)) {
			//调用图灵机器人
			resqContext = TuLinService.tulinResult(map.get("Content"));
		}

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值