哄骗微信大众,平台实现主动答复消息—java版

    添加时间:2013-8-1 点击量:

    比来公司须要拿微信大众,平台做个器材,所以就开端了最根蒂根基进修,网上很多是php版的,对于我这个只会java,不会php的就只能在网上找点只言片语来一点一点进修了。不费话了直接贴图看结果(很简单的)。



    不会做存眷时就答复信息,所以必须先发送“?”,才干呈现提示。答复“1”和“其他”都是返回文字信息,“2”是音乐信息,“3”是图文信息。


    好了,下面进入正题。


    1、起首必须开通微信公中平台,还得申请到高等功能的(这个点斗劲坑,我等了两天才申请到的),关于申请大众,平台我就不说了,网上很多介绍,本身看吧。


    2、必须有本身的办事器资料,因为大众,平台开辟者模式开启时,必须填写接口设备信息(成为开辟者)。



    办事器资料我是用的是百度开辟者中间来创建本身的应用。百度开辟者中间:http://developer.baidu.com/


    成为微信大众,平台开辟者前必须公布好本身的办事器资料,因为微信办事器将发送GET恳求到填写的URL上(后面具体介绍)。


    3、创建本身的应用。



     这是全部项目布局。


    下面一一介绍:




      1 public class WbChat extends HttpServlet{
    
    2
    3 private static final long serialVersionUID = 1L;
    4
    5 public WbChat() {
    6 super();
    7 }
    8 @Override
    9 protected void doGet(HttpServletRequest request, HttpServletResponse response)
    10 throws ServletException, IOException {
    11 //这里没有对发送信息者进行验证,直接返回了,要加验证的话本身去百度吧
    12 response.setContentType(text/html;charset=UTF-8);
    13 PrintWriter pw = response.getWriter();
    14 String echo = request.getParameter(echostr);
    15 echo = new String(echo.getBytes(ISO-8859-1),UTF-8);
    16 pw.println(echo);
    17 //当你提交成为开辟者的URL和Token时,微信办事器将发送GET恳求到填写的URL上,
    18 //只有你返回参数echostr时,他才会认为你这个接口是通的,才会提交成功
    19 }
    20
    21 @Override
    22 protected void doPost(HttpServletRequest request, HttpServletResponse response)
    23 throws ServletException, IOException {
    24 // 当你用微信给平台发送信息时就会到这里
    25 // 答复音乐和图文消息,我都写死了,本身可以按照本身的须要加响应的处理惩罚
    26 response.setContentType(text/html;charset=UTF-8);
    27 PrintWriter pw = response.getWriter();
    28 String wxMsgXml = IOUtils.toString(request.getInputStream(),utf-8);
    29 RequestTextMessage textMsg = null;
    30 try {
    31 textMsg = getRequestTextMessage(wxMsgXml);
    32 } catch (Exception e) {
    33 e.printStackTrace();
    34 }
    35 StringBuffer replyMsg = new StringBuffer();
    36 String receive = textMsg.getContent().trim();
    37 String returnXml = null;
    38
    39 if (textMsg != null&&!receive.equals()) {
    40 if (receive.equals(?)||receive.equals(?)) {
    41
    42 replyMsg.append(迎接应用微信平台!);
    43 replyMsg.append(\r\n1、当前时候);
    44 replyMsg.append(\r\n2、听音乐);
    45 replyMsg.append(\r\n3、看图文);
    46 replyMsg.append(\r\n其他、覆信壁请直接输入文字信息);
    47
    48 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
    49
    50 } else if (receive.equals(2)) {
    51
    52 //答复音乐信息
    53 returnXml = getReplyMusicMessage(textMsg.getFromUserName(),textMsg.getToUserName());
    54
    55
    56 } else if (receive.equals(3)) {
    57
    58 //答复图文
    59 returnXml = getReplyTuwenMessage(textMsg.getFromUserName(),textMsg.getToUserName());
    60
    61 } else if (receive.equals(1)) {
    62
    63 //答复时候
    64 SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
    65
    66 replyMsg.append(当前时候\r\n+df.format(new Date()));
    67 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
    68
    69 } else {
    70
    71 replyMsg.append(收到: + textMsg.getContent());
    72 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
    73
    74 }
    75 } else {
    76
    77 replyMsg.append(别闹了,这谁看得懂啊!);
    78 returnXml = getReplyTextMessage(replyMsg.toString(), textMsg.getFromUserName(),textMsg.getToUserName());
    79
    80 }
    81 pw.println(returnXml);
    82 }
    83
    84
    85 // 获取推送文本消息
    86 private RequestTextMessage getRequestTextMessage(String xml){
    87
    88 XStream xstream = new XStream(new DomDriver());
    89
    90 xstream.alias(xml, RequestTextMessage.class);
    91 xstream.aliasField(ToUserName, RequestTextMessage.class, toUserName);
    92 xstream.aliasField(FromUserName, RequestTextMessage.class, UserName);
    93 xstream.aliasField(CreateTime, RequestTextMessage.class, createTime);
    94 xstream.aliasField(MsgType, RequestTextMessage.class, messageType);
    95 xstream.aliasField(Content, RequestTextMessage.class, content);
    96 xstream.aliasField(MsgId, RequestTextMessage.class, msgId);
    97
    98 RequestTextMessage requestTextMessage = (RequestTextMessage)xstream.XML(xml);
    99 return requestTextMessage;
    100 }
    101 // 答复文本消息
    102 private String getReplyTextMessage(String content, String UserName,String toUserName){
    103
    104 ReplyTextMessage we = new ReplyTextMessage();
    105 we.setMessageType(text);
    106 we.setFuncFlag(0);
    107 we.setCreateTime(new Long(new Date().getTime()).toString());
    108 we.setContent(content);
    109 we.setToUserName(UserName);
    110 we.setFromUserName(toUserName);
    111 XStream xstream = new XStream(new DomDriver());
    112 xstream.alias(xml, ReplyTextMessage.class);
    113 xstream.aliasField(ToUserName, ReplyTextMessage.class, toUserName);
    114 xstream.aliasField(FromUserName, ReplyTextMessage.class, UserName);
    115 xstream.aliasField(CreateTime, ReplyTextMessage.class, createTime);
    116 xstream.aliasField(MsgType, ReplyTextMessage.class, messageType);
    117 xstream.aliasField(Content, ReplyTextMessage.class, content);
    118 xstream.aliasField(FuncFlag, ReplyTextMessage.class, funcFlag);
    119 String xml =xstream.toXML(we);
    120 return xml;
    121 }
    122 // 答复音乐消息
    123 private String getReplyMusicMessage(String UserName,String toUserName){
    124
    125 ReplyMusicMessage we = new ReplyMusicMessage();
    126 Music music = new Music();
    127
    128 we.setMessageType(music);
    129 we.setCreateTime(new Long(new Date().getTime()).toString());
    130 we.setToUserName(UserName);
    131 we.setFromUserName(toUserName);
    132 we.setFuncFlag(0);
    133
    134 music.setTitle(回家|X-man);
    135 music.setDescription(萨克斯 回家 勾起你想家的愁思...);
    136
    137 String url = http://bcs.duapp.com/yishi-music/%E5%9B%9E%E5%AE%B6.mp3?sign=MBO:97068c69ccb2ab230a497c59d528dcce:LdYZ%2FLXohKa6YCy9gbxL%2B1mZ4Co%3D;
    138 String url2 = http://bcs.duapp.com/yishi-music/X-man.mp3?sign=MBO:97068c69ccb2ab230a497c59d528dcce:cYV%2B%2Fq2Tlv2de6gqecZynCyIm3k%3D;
    139 music.setMusicUrl(url);
    140 music.setHqMusicUrl(url2);
    141
    142 we.setMusic(music);
    143
    144 XStream xstream = new XStream(new DomDriver());
    145 xstream.alias(xml, ReplyMusicMessage.class);
    146 xstream.aliasField(ToUserName, ReplyMusicMessage.class, toUserName);
    147 xstream.aliasField(FromUserName, ReplyMusicMessage.class, UserName);
    148 xstream.aliasField(CreateTime, ReplyMusicMessage.class, createTime);
    149 xstream.aliasField(MsgType, ReplyMusicMessage.class, messageType);
    150 xstream.aliasField(FuncFlag, ReplyMusicMessage.class, funcFlag);
    151 xstream.aliasField(Music, ReplyMusicMessage.class, Music);
    152
    153 xstream.aliasField(Title, Music.class, title);
    154 xstream.aliasField(Description, Music.class, description);
    155 xstream.aliasField(MusicUrl, Music.class, musicUrl);
    156 xstream.aliasField(HQMusicUrl, Music.class, hqMusicUrl);
    157
    158 String xml =xstream.toXML(we);
    159 return xml;
    160 }
    161
    162 // 答复图文消息
    163 private String getReplyTuwenMessage(String UserName,String toUserName){
    164
    165 ReplyTuwenMessage we = new ReplyTuwenMessage();
    166
    167 Articles articles = new Articles();
    168
    169 Item item = new Item();
    170
    171 we.setMessageType(news);
    172 we.setCreateTime(new Long(new Date().getTime()).toString());
    173 we.setToUserName(UserName);
    174 we.setFromUserName(toUserName);
    175 we.setFuncFlag(0);
    176 we.setArticleCount(1);
    177
    178 item.setTitle(俊介);
    179 item.setDescription(俊介(SHUNSUKE)是Twitter上如今最风行的偶像犬,是哈多利系博美犬(即俗称英系博美),因为在网上卖萌而走红收集。);
    180 item.setPicUrl(http://bcs.duapp.com/yishi-music/111.jpg?sign=MBO:97068c69ccb2ab230a497c59d528dcce:hmzcBYxgI4yUaTd9GvahO1GvE%2BA%3D);
    181 item.setUrl(http://baike.baidu.com/view/6300265.htm);
    182
    183 articles.setItem(item);
    184 we.setArticles(articles);
    185
    186 XStream xstream = new XStream(new DomDriver());
    187 xstream.alias(xml, ReplyTuwenMessage.class);
    188 xstream.aliasField(ToUserName, ReplyTuwenMessage.class, toUserName);
    189 xstream.aliasField(FromUserName, ReplyTuwenMessage.class, UserName);
    190 xstream.aliasField(CreateTime, ReplyTuwenMessage.class, createTime);
    191 xstream.aliasField(MsgType, ReplyTuwenMessage.class, messageType);
    192 xstream.aliasField(Articles, ReplyTuwenMessage.class, Articles);
    193
    194 xstream.aliasField(ArticleCount, ReplyTuwenMessage.class, articleCount);
    195 xstream.aliasField(FuncFlag, ReplyTuwenMessage.class, funcFlag);
    196
    197 xstream.aliasField(item, Articles.class, item);
    198
    199 xstream.aliasField(Title, Item.class, title);
    200 xstream.aliasField(Description, Item.class, description);
    201 xstream.aliasField(PicUrl, Item.class, picUrl);
    202 xstream.aliasField(Url, Item.class, url);
    203
    204 String xml =xstream.toXML(we);
    205 return xml;
    206 }
    207
    208 }


    WbChat



     1 public class ReplyMusicMessage {
    
    2
    3 private String toUserName;
    4 private String UserName;
    5 private String createTime;
    6 private String messageType;
    7 private Music Music;
    8 private String funcFlag;
    9 //这里的funcFlag,微信平台接口文档里没有,然则必须写上,不然会收不到返回的信息
    10 //getters、setters
    11 }


    ReplyMusicMessage



     1 public class ReplyTextMessage {
    
    2
    3 private String toUserName;
    4 private String UserName;
    5 private String createTime;
    6 private String messageType;
    7 private String content;
    8 private String funcFlag;
    9 // getters,setters
    10 }


    ReplyTextMessage



     1 public class ReplyTuwenMessage {
    
    2
    3 private String toUserName;
    4 private String UserName;
    5 private String createTime;
    6 private String messageType;
    7 private int articleCount;
    8
    9 private Articles Articles;
    10 private String funcFlag;
    11 //getters,setters
    12 }


    ReplyTuwenMessage



    1 public class RequestTextMessage {
    
    2 private String toUserName;
    3 private String UserName;
    4 private String createTime;
    5 private String messageType;
    6 private String content;
    7 private String msgId;
    8 //getters,setters
    9 }


    RequestTextMessage



     1 public class Articles {
    
    2
    3 private Item item;
    4
    5 public Item getItem() {
    6 return item;
    7 }
    8
    9 public void setItem(Item item) {
    10 this.item = item;
    11 }
    12
    13
    14 }


    Articles



    1 public class Item {
    
    2
    3 private String title;
    4 private String description;
    5 private String picUrl;
    6 private String url;
    7 //getters,setters
    8 }


    Item



    1 public class Music {
    
    2
    3 private String title;
    4 private String description;
    5 private String musicUrl;
    6 private String hqMusicUrl;
    7 //getters,setters
    8 }


    Music



    1 <servlet>   
    
    2 <servlet-name>Test</servlet-name>
    3 <servlet-class>com.main.WbChat</servlet-class>
    4 </servlet>
    5 <servlet-mapping>
    6 <servlet-name>Test</servlet-name>
    7 <url-pattern>/wbchat</url-pattern>
    8 </servlet-mapping>


    web.xml设备

    还要加四个jar包:


     哄骗MyEclipse直接把打为war包,上传到百度开辟者中间,并上线该应用。


    4、成为开辟者,开启开辟模式。



    将百度开辟者中间中的当前域名+/wbchat 添加到微信平台的URL处,Token可以随便填(因为我没有断定Token)。


    5、大功成功,可以在微信上添加该大众,账号聊天了。




    小提示:


    1、调试时,微信发送消息却没有受到信息,可以去看看百度开辟者中间,在应用版本经管中找到该应用,然后点击高等下的日记,查看日记来看出什么题目了


    2、jar包地址:http://jarfiles.pandaidea.com/


    3、不要听百度开辟者中间说的那个”如何提交代码“,它是应用svn,它给的svn地址下的是svn1.8的,底子就checkout不了,若是想应用svn的话,就得去svn1.6的版底本应用。我是应用上传更新war包来更新的,如许在本身的MyEclipse中直接编写。

    文艺不是炫耀,不是花哨空洞的文字堆砌,不是一张又一张的逆光照片,不是将旅行的意义转化为名牌包和明信片的物质展示;很多时候它甚至完全不美——它嘶吼、扭曲,它会痛苦地抽搐,它常常无言地沉默。——艾小柯《文艺是一种信仰》
    分享到: