基于YMP-WeChat模块接收并处理微信事件及消息的代码

public class MessageHandler implements IMessageHandler {

	private static final Map<String, PairObject<String, Long>> __resources = new ConcurrentHashMap<String, PairObject<String, Long>>();

	public OutMessage onTextMessage(ITextMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		_msg.setContent("您发了一段文字: " + message.getContent());
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onImageMessage(IImageMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		_msg.setContent("您发了一张图片: " + message.getMediaId() + "   " + message.getPicUrl());
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onVoiceMessage(IVoiceMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		_msg.setContent("您发了一段语音: " + message.getMediaId() + "  "+ message.getRecognition());
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onVideoMessage(IVideoMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		_msg.setContent("您发了一段视频: " + message.getMediaId());
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onLocationMessage(ILocationMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		_msg.setContent("您分享的位置: " + message.getLabel() + "\n\r" + message.getX() + "," + message.getY() + "," + message.getScale());
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onLinkMessage(ILinkMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		_msg.setContent("您发了一个连接: " + message.getTitle() + "  " + message.getUrl());
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onEventMessage(InMessage message) throws Exception {
		TextOutMessage _msg = new TextOutMessage(message.getFromUserName());
		_msg.setFromUserName(message.getToUserName());
		//
		XStream _xstream = XStreamHelper.createXStream(true);
		_xstream.processAnnotations(message.getClass());
		//
		_msg.setContent("收到一条未知类型的消息报文:" + _xstream.toXML(message));
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onEventSubscribe(ISubscribeEvent event) throws Exception {
		TextOutMessage _msg = new TextOutMessage(event.getFromUserName());
		_msg.setFromUserName(event.getToUserName());
		_msg.setContent("非常感谢您对我们的关注,更新信息请访问https://github.com/suninformation");
		_msg.setCreateTime(System.currentTimeMillis());
		return _msg;
	}

	public OutMessage onEventUnsubscribe(IUnsubscribeEvent event) throws Exception {
		Logs.debug(event.getFromUserName() + "取消了关注");
		return null;
	}

	public OutMessage onEventClick(final IClickEvent event) throws Exception {
		if (event.getEventKey().equalsIgnoreCase("TEST_MSG_TEXT")) {
			TextOutMessage _msg = new TextOutMessage(event.getFromUserName());
			_msg.setContent("您好! 感谢您对我们的关注, 更新信息请访问https://github.com/suninformation!");
			WeChat.wxMessageSendCustom(_msg);
		} else if (event.getEventKey().equalsIgnoreCase("TEST_MSG_IMAGE")) {
			PairObject<String, Long> _res = __resources.get("IMAGE");
			if (_res == null || (System.currentTimeMillis() - _res.getValue()) > (1000*60*60*24*2)) {
				WxMediaUploadResult _result = WeChat.wxMediaUploadFile(WxMediaType.IMAGE, new File(RuntimeUtils.getRootPath(), "resources/image.jpg"));
				System.out.println(JSON.toJSONString(_result, true));
				__resources.put("IMAGE", new PairObject<String, Long>(_result.getMediaId(), _result.getCreatedAt()));
				_res = __resources.get("IMAGE");
			}
			ImageOutMessage _msg = new ImageOutMessage(event.getFromUserName());
			_msg.setImage(new MediaId(_res.getKey()));
			WeChat.wxMessageSendCustom(_msg);
		} else if (event.getEventKey().equalsIgnoreCase("TEST_MSG_VOICE")) {
			PairObject<String, Long> _res = __resources.get("VOICE");
			if (_res == null || (System.currentTimeMillis() - _res.getValue()) > (1000*60*60*24*2)) {
				WxMediaUploadResult _result = WeChat.wxMediaUploadFile(WxMediaType.VOICE, new File(RuntimeUtils.getRootPath(), "resources/demo.amr"));
				System.out.println(JSON.toJSONString(_result, true));
				__resources.put("VOICE", new PairObject<String, Long>(_result.getMediaId(), _result.getCreatedAt()));
				_res = __resources.get("VOICE");
			}
			VoiceOutMessage _msg = new VoiceOutMessage(event.getFromUserName());
			_msg.setVoice(new MediaId(_res.getKey()));
			WeChat.wxMessageSendCustom(_msg);
		} else if (event.getEventKey().equalsIgnoreCase("TEST_MSG_VIDEO")) {
			PairObject<String, Long> _res = __resources.get("VIDEO");
			if (_res == null || (System.currentTimeMillis() - _res.getValue()) > (1000*60*60*24*2)) {
				WxMediaUploadResult _result = WeChat.wxMediaUploadFile(WxMediaType.VIDEO, new File(RuntimeUtils.getRootPath(), "resources/demo.mp4"));
				System.out.println(JSON.toJSONString(_result, true));
				__resources.put("VIDEO", new PairObject<String, Long>(_result.getMediaId(), _result.getCreatedAt()));
				_res = __resources.get("VIDEO");
			}
			VideoOutMessage _msg = new VideoOutMessage(event.getFromUserName());
			MediaId _v = new MediaId(_res.getKey());
			_v.setTitle("测试视频");
			_v.setDescription("测试视频描述");
			_msg.setVideo(_v);
			WeChat.wxMessageSendCustom(_msg);
		} else if (event.getEventKey().equalsIgnoreCase("TEST_MSG_MUSIC")) {
			PairObject<String, Long> _res = __resources.get("THUMB");
			if (_res == null || (System.currentTimeMillis() - _res.getValue()) > (1000*60*60*24*2)) {
				WxMediaUploadResult _result = WeChat.wxMediaUploadFile(WxMediaType.THUMB, new File(RuntimeUtils.getRootPath(), "resources/image.jpg"));
				System.out.println(JSON.toJSONString(_result, true));
				__resources.put("THUMB", new PairObject<String, Long>(_result.getThubmMediaId(), _result.getCreatedAt()));
				_res = __resources.get("THUMB");
			}
			MusicOutMessage _msg = new MusicOutMessage(event.getFromUserName());
			MediaId _music = new MediaId();
			_music.setMusicUrl("http://localhost/images/RainandTears.mp3");
			_music.setHqMusicUrl(_music.getMusicUrl());
			_music.setTitle("Rain and Tears");
			_music.setDescription("经典老歌...");
			_music.setThumbMediaId(_res.getKey());
			_msg.setMusic(_music);
			WeChat.wxMessageSendCustom(_msg);
		} else if (event.getEventKey().equalsIgnoreCase("TEST_MSG_NEWS")) {
			PairObject<String, Long> _res = __resources.get("IMAGE");
			if (_res == null || (System.currentTimeMillis() - _res.getValue()) > (1000*60*60*24*2)) {
				WxMediaUploadResult _result = WeChat.wxMediaUploadFile(WxMediaType.IMAGE, new File(RuntimeUtils.getRootPath(), "resources/image.jpg"));
				System.out.println(JSON.toJSONString(_result, true));
				__resources.put("IMAGE", new PairObject<String, Long>(_result.getMediaId(), _result.getCreatedAt()));
				_res = __resources.get("IMAGE");
			}
			NewsOutMessage _msg = new NewsOutMessage(event.getFromUserName());
			Article _a = new Article();
			_a.setTitle("\"去哪儿\"很忙~~");
			_a.setPicUrl("http://localhost/images/image.jpg");
			_a.setUrl("https://github.com/suninformation");
			_msg.getArticles().add(_a);
			//
			Article _b = new Article();
			_b.setTitle("爸爸\"去哪儿\"~~");
			_msg.getArticles().add(_b);
			//
			Article _c = new Article();
			_c.setTitle("飞机\"去哪儿\"~~");
			_msg.getArticles().add(_c);
			//
			WeChat.wxMessageSendCustom(_msg);
		} else if (event.getEventKey().equalsIgnoreCase("TEST_API_USERINFO")) {
			WxUser _u = WeChat.wxUserGetInfo(event.getFromUserName(), null);
			NewsOutMessage _msg = new NewsOutMessage(event.getFromUserName());
			Article _a = new Article();
			_a.setTitle("用户基本信息: ");
			_a.setPicUrl(_u.getHeadImgUrl());
			StringBuilder _sb = new StringBuilder();
			_sb.append("昵称:").append(_u.getNickname()).append("\n\r");
			_sb.append("性别:").append(_u.getSex() == 1 ? "男" : "女").append("\n\r");
			_sb.append("城市:").append(_u.getCountry()).append(_u.getProvince()).append(_u.getCity()).append("\n\r");
			_sb.append("关注时间:").append(DateTimeUtils.formatTime(_u.getSubscribeTime() * 1000, null)).append("\n\r");
			_a.setDescription(_sb.toString());
			_msg.getArticles().add(_a);
			//
			WeChat.wxMessageSendCustom(_msg);
		} 
		return null;
	}

	public OutMessage onEventView(IViewEvent event) throws Exception {
		TextOutMessage _msg = new TextOutMessage(event.getFromUserName());
		_msg.setContent("您查看了网页: " + event.getEventKey());
		_msg.setCreateTime(System.currentTimeMillis());
		WeChat.wxMessageSendCustom(_msg);
		return null;
	}

	public OutMessage onEventScan(IScanEvent event) throws Exception {
		TextOutMessage _msg = new TextOutMessage(event.getFromUserName());
		_msg.setContent("您扫描了一张二维码: " + event.getEventKey() + "  " + event.getTicket());
		_msg.setCreateTime(System.currentTimeMillis());
		WeChat.wxMessageSendCustom(_msg);
		return null;
	}

	public OutMessage onEventLocation(ILocationEvent event) throws Exception {
		Logs.debug(event.getFromUserName() + "上报位置: " + event.getLatitude() + "," + event.getLongitude() + "," + event.getPrecision());
		TextOutMessage _msg = new TextOutMessage(event.getFromUserName());
		_msg.setContent("您当前上报的位置: " + event.getLatitude() + "," + event.getLongitude() + "," + event.getPrecision());
		_msg.setCreateTime(System.currentTimeMillis());
		WeChat.wxMessageSendCustom(_msg);
		return null;
	}

}



你可能感兴趣的:(微信,YMP,YMP-WeChat)