您的位置:首页 > 移动开发 > 微信开发

java版微信调用小i机器人

2014-05-08 17:17 169 查看
为了工作之余无聊时找个陪聊天的机器人放松放松,我把和机器人聊天放入微信系统中,用户使用过程中,可输入信息与机器人聊天,也可点击自助菜单进入系统工作。



小i机器人官网:
http://cloud.xiaoi.com/agreement.jsp
注册后如下图:



用户注册申请后,自动为初级授权用户,当用户申请实名认证后为高级授权用户。
初级授权没有过期时间,高级授权则只有5个月的免费调用时间。权限如下:





看看我们关心的接口调用地方:快速接入-api









上面的开发者凭证和签名生成是我们在程序要用到的。

接下来就看看如何调用小i机器人:
注:看不懂代码建议看看柳峰老师的博客。

try {
Map<String, String> requestMap = MessageUtil.parseXml(request);

String toUserName = requestMap.get("FromUserName");
String fromUserName = requestMap.get("ToUserName");
String msgType = requestMap.get("MsgType");

TextMessage textMessage =new TextMessage();
textMessage.setCreateTime(new Date().getTime());
textMessage.setToUserName(toUserName);
textMessage.setFromUserName(fromUserName);
textMessage.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_TEXT);
// textMessage.setFuncFlag(0);

if(MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType)){
String content = requestMap.get("Content");
String
createTime = requestMap.get("CreateTime");
System.out.println(createTime);

//自己的机器人不够智能停用
//respContent =ChatService.chat(fromUserName, createTime, content);

//调用小i机器人 xxx为申请小i机器人的用户名
respContent = Chat4XiaoI.Tess(content,"xxx");
}

这里是微信调用小i机器人入口关键部分:

//调用小i机器人 xxx为申请小i机器人的用户名
respContent = Chat4XiaoI.Tess(content,"xxx");

下面是如何调用小i机器人,直接给出代码,不做解释:

package com.gxtk.weixin.utils;

import java.io.IOException;
import java.util.Random;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;

/**
*调用小i机器人处理类
*
*@author荒唐漫步者
*@date2014-05-07
*
*/
publicclassChat4XiaoI{

//用户申请的key
privatefinalstatic StringAPP_KEY
="xxx";
//用户申请的secret
privatefinalstatic StringAPP_SECRET
="xxx";

/**
*调用小i机器人
*
*@paramkey 对应微信的content,也就是用户发送的内容
*@paramnames 小i用户名
*@return返回回答结果
*/
publicstatic String Tess(String key, String names) {

String realm =
"xiaoi.com";
String method =
"POST";
String uri =
"/robot/ask.do";
byte[] b =newbyte[20];
newRandom().nextBytes(b);
String nonce = new String(Hex.encodeHex(b));

String HA1 = DigestUtils.shaHex(StringUtils.join(new String[] {APP_KEY,
realm,APP_SECRET },":"));

String HA2 =DigestUtils.shaHex(StringUtils.join(new String[] { method,uri },":"));

String sign =DigestUtils.shaHex(StringUtils.join(new String[] { HA1, nonce, HA2 },":"));

String str = null;

HttpClient hc =
new
HttpClient();
PostMethod pm =
new
PostMethod("http://nlp.xiaoi.com/robot/ask.do");
pm.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");

//key换成自己用户key
pm.addRequestHeader("X-Auth","app_key=\"xxx\",nonce=\"" + nonce +"\",
signature=\"" + sign +"\"");
pm.setParameter("platform","weixin");
pm.setParameter("type","0");
pm.setParameter("userId", names);
pm.setParameter("question", key);
int re_code;
try {
re_code =hc.executeMethod(pm);
if (re_code == 200) {
str =pm.getResponseBodyAsString();
}
} catch (HttpException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}

return str;

}

聊天截图:





可以修改机器人名称:在小i机器人官网修改:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: