Coder Social home page Coder Social logo

java-weixinlib's Introduction

java-weixinlib

微信公众平台接入

使用了jdom作为xml解析输出框架

使用说明:

// 接入时
final String TOKEN = "xxx";
String signature = getRequest.getParameter("signature");
String timestamp = getRequest.getParameter("timestamp");
String nonce = getRequest.getParameter("nonce");
String echostr = getRequest.getParameter("echostr");
	
if(null != timestamp && null != nonce && null != echostr && null != signature) {
	if(WeiXin.access(TOKEN, signature, timestamp, nonce)) {
		getResponse().getWriter().write(echostr);
		return;
	}
	return;
}

=======================

// 接收消息
WxRecvMsg msg = WeiXin.recv(getRequest().getInputStream());
WxSendMsg sendMsg = WeiXin.builderSendByRecv(msg);
System.out.println(msg);
// 微信事件消息, 关注/取消关注/菜单...
if(msg instanceof WxRecvEventMsg) {
	WxRecvEventMsg m = (WxRecvEventMsg) msg;
	String event = m.getEvent();
	// 有人关注微信帐号
	if("subscribe".equals(event)) {
		String content = "欢迎关注xxx";
		// 构建文本消息进行发送
		sendMsg = new WxSendTextMsg(sendMsg, content);
		// 发送回微信
		WeiXin.send(sendMsg, getResponse().getOutputStream());
	}
	return;
}
// 文本消息..,目前支持的消息有(WxRecvEventMsg/事件消息,WxRecvGeoMsg/地理位置消息,WxRecvLinkMsg/连接消息,WxRecvPicMsg/图片消息)
if(msg instanceof WxRecvTextMsg)


// 发送消息构建
// 通过 WxSendMsg sendMsg = WeiXin.builderSendByRecv(msg); 将收到的消息转为发送消息(交换了sendUser和fromUser)
// 文本消息
new WxSendTextMsg(sendMsg, content);

// 多图消息
WxSendNewsMsg newsMsg = new WxSendNewsMsg(sendMsg)
				.addItem("标题", "描述", "图片地址", "点击后跳转的链接")
				.addItem....
				最多可以添加10个
// 音乐消息
new WxSendMusicMsg(sendMsg, "标题","描述","低品质音乐地址", "高品质音乐地址 (wifi环境会使用这个地址进行播放)");

// 发送这些消息的时候可以直接使用`WeiXin.send(sendMsg, getResponse().getOutputStream());`会将msg转换成xml输出

java-weixinlib's People

Contributors

0x0001 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.