java版微信调用小i机器人

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


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机器人官网修改:


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值