- 概述
- 文字处理
- 语音处理
- 其他事件
- 附录
概述
原文链接:http://blog.csdn.net/u011506468/article/details/47280443
公众号 SuperLishun,想要源码的可以关注公众号,给我留言。
微信服务器返回的数据都为如下格式
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你好]]></Content>
</xml>
先将其解析为java对象再处理,通过判断MsgType这个字段来判断消息的类型,进而进一步根据自己的需求进行处理。
/* type为上述MsgType */
switch (type)
{
case "text":
oms = wechatService.textTypeMsg(msg, wechatUser);
break;
case "image":
oms = wechatService.imageTypeMsg(msg, wechatUser);
break;
case "voice":
oms = wechatService.voiceTypeMsg(msg, wechatUser);
break;
case "video":
oms = wechatService.videoTypeMsg(msg, wechatUser);
break;
case "location":
oms = wechatService.locationTypeMsg(msg, wechatUser);
break;
case "link":
oms = wechatService.linkTypeMsg(msg, wechatUser);
break;
case "event":
oms = wechatService.eventTypeMsg(msg, wechatUser);
break;
case "news":
oms = wechatService.eventNewsMsg(msg, wechatUser);
break;
default:
break;
}
文字处理
通过msg的content内容获得输入的字符串,通过OutMessage对象的content字段将回复内容输出,InMessage和OutMessage的数据格式会在最下面给出。WeChatUser暂时不需要,为前面封装的微信用户信息对象。处理方法代码示例如下
/**
* 文字内容的消息处理
* @param msg 接收信息
* @param wechatUser 用户信息
* @return 回复信息
*/
public OutMessage textTypeMsg(InMessage msg, WechatUser wechatUser)
{
log.info(DateUtil.format(new Date(), DateUtil.FMT_S_TIMES)+"|" + msg.getContent() +"|文字内容的消息处理");
OutMessage out = null;
String str = msg.getContent().toLowerCase();
out.setContent("您输入的是:" + str);
return out;
}
语音处理
将微信开发者中心的语音识别接口开启,则可以接受已经被语音识别后的语音,代码差不多,只不过是接受输入字符串的变量变了,为recognition字段
/**
* 语音的消息处理
* @param msg 接收信息
* @param wechatUser 用户信息
* @return 回复信息
*/
public OutMessage textTypeMsg(InMessage msg, WechatUser wechatUser)
{
log.info(DateUtil.format(new Date(), DateUtil.FMT_S_TIMES)+"|" + msg.getContent() +"|语音内容的消息处理");
OutMessage out = null;
String str = msg.getRecognition().toLowerCase();
out.setContent("您说的是:" + str);
return out;
}
其他事件
有时间在写
附录
InMessage
import java.io.Serializable;
/**
* 接收消息实体类
* @author lishun
*
*/
public class InMessage implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -2572635563423558942L;
/** 开发者微信号 */
private String ToUserName;
//private String Encrypt;
/** 发送方帐号OpenID */
private String FromUserName;
/** 消息创建时间 */
private Long CreateTime;
/** text,image,location,link,event,voice,video */
private String MsgType = "text";
/** 消息ID */
private Long MsgId;
/** 文本消息内容 */
private String Content;
/** 图片链接 */
private String PicUrl;
/** 地理位置纬度 */
private String Location_X;
/** 地理位置经度 */
private String Location_Y;
/** 地图缩放大小 */
private Long Scale;
/** 地理位置信息 */
private String Label;
/** 消息标题 */
private String Title;
/** 消息描述 */
private String Description;
/** 消息链接 */
private String Url;
/** subscribe,unsubscribe,subscribe,scan,location,click */
private String Event;
/** 事件KEY值,与自定义菜单接口中KEY值对应 */
private String EventKey;
/** 二维码的ticket,可用来换取二维码图片 */
private String Ticket;
/** 语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体 */
private String MediaId;
/** 语音格式:amr */
private String Format;
/** 语音识别结果,UTF8编码 */
private String Recognition;
/** 消息id,64位整型 */
private String MsgID;
private String KfAccount;
private String FromKfAccount;
private String ToKfAccount;
public String getFromKfAccount() {
return FromKfAccount;
}
public void setFromKfAccount(String fromKfAccount) {
FromKfAccount = fromKfAccount;
}
public String getToKfAccount() {
return ToKfAccount;
}
public void setToKfAccount(String toKfAccount) {
ToKfAccount = toKfAccount;
}
private TransInfo TransInfo;
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
this.ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
this.FromUserName = fromUserName;
}
public Long getCreateTime() {
return CreateTime;
}
public void setCreateTime(Long createTime) {
this.CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
this.MsgType = msgType;
}
public Long getMsgId() {
return MsgId;
}
public void setMsgId(Long msgId) {
this.MsgId = msgId;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
this.Content = content;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
this.PicUrl = picUrl;
}
public String getLocation_X() {
return Location_X;
}
public void setLocation_X(String location_X) {
this.Location_X = location_X;
}
public String getLocation_Y() {
return Location_Y;
}
public void setLocation_Y(String location_Y) {
this.Location_Y = location_Y;
}
public Long getScale() {
return Scale;
}
public void setScale(Long scale) {
this.Scale = scale;
}
public String getLabel() {
return Label;
}
public void setLabel(String label) {
this.Label = label;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
this.Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
this.Description = description;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
this.Url = url;
}
public String getEvent() {
return Event;
}
public void setEvent(String event) {
this.Event = event;
}
public String getEventKey() {
return EventKey;
}
public void setEventKey(String eventKey) {
this.EventKey = eventKey;
}
public String getTicket() {
return Ticket;
}
public void setTicket(String ticket) {
Ticket = ticket;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getFormat() {
return Format;
}
public void setFormat(String format) {
Format = format;
}
public String getRecognition() {
return Recognition;
}
public void setRecognition(String recognition) {
Recognition = recognition;
}
public String getMsgID() {
return MsgID;
}
public void setMsgID(String msgID) {
MsgID = msgID;
}
public TransInfo getTransInfo() {
return TransInfo;
}
public void setTransInfo(TransInfo transInfo) {
TransInfo = transInfo;
}
public String getKfAccount() {
return KfAccount;
}
public void setKfAccount(String kfAccount) {
KfAccount = kfAccount;
}
// public String getEncrypt() {
// return Encrypt;
// }
// public void setEncrypt(String encrypt) {
// Encrypt = encrypt;
// }
}
OutMessage
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 回复消息实体类
* @author lishun
*
*/
public class OutMessage implements Serializable
{
/** 接收方帐号OpenID */
private String ToUserName;
/** 开发者微信号 */
private String FromUserName;
/** 消息创建时间 */
private Long CreateTime;
/** text,image,voice,video,music,news */
private String MsgType = "text";
/** 消息ID */
private Long msgId;
/** 回复的消息内容长度不超过2048字节 */
private String Content;
private int FuncFlag = 0;
/** 通过上传多媒体文件得到的ID */
public String MediaId;
/** 缩略图的媒体ID通过上传多媒体文件得到的ID */
public String ThumbMediaId;
/** 音乐链接 */
private String MusicUrl;
/** 高质量音乐链接 */
private String HQMusicUrl;
/** 图文消息个数限制为10条以内 */
private Integer ArticleCount;
/** 图文消息标题 */
private String Title;
/** 图文消息描述 */
private String Description;
/** 图片链接支持JPG、PNG格式较好的效果为大图640*320小图80*80 */
private String PicUrl;
/** 点击图文消息跳转链接 */
private String Url;
private TransInfo TransInfo;
private List<Articles> Articles;
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public Long getCreateTime() {
return CreateTime;
}
public void setCreateTime(Long createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
public Long getMsgId() {
return msgId;
}
public void setMsgId(Long msgId) {
this.msgId = msgId;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public int getFuncFlag() {
return FuncFlag;
}
public void setFuncFlag(int funcFlag) {
FuncFlag = funcFlag;
}
public String getMediaId() {
return MediaId;
}
public void setMediaId(String mediaId) {
MediaId = mediaId;
}
public String getThumbMediaId() {
return ThumbMediaId;
}
public void setThumbMediaId(String thumbMediaId) {
ThumbMediaId = thumbMediaId;
}
public String getMusicUrl() {
return MusicUrl;
}
public void setMusicUrl(String musicUrl) {
MusicUrl = musicUrl;
}
public String getHQMusicUrl() {
return HQMusicUrl;
}
public void setHQMusicUrl(String hQMusicUrl) {
HQMusicUrl = hQMusicUrl;
}
public Integer getArticleCount() {
return ArticleCount;
}
public void setArticleCount(Integer articleCount) {
ArticleCount = articleCount;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getPicUrl() {
return PicUrl;
}
public void setPicUrl(String picUrl) {
PicUrl = picUrl;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public List<Articles> getArticles() {
return Articles;
}
public void setArticles(List<Articles> articles) {
if (articles.size() > 10)
articles = new ArrayList<Articles>(articles.subList(0, 10));
this.Articles = articles;
}
public TransInfo getTransInfo() {
return TransInfo;
}
public void setTransInfo(TransInfo transInfo) {
TransInfo = transInfo;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-29 04:09:43