Skip to content

主动发送消息

Javen205 edited this page Dec 20, 2015 · 1 revision

直接使用SendMessageApi.sendxxx(xx)

     /**
	 * 发送文本消息
	 */
	public void sendTextMssage() {
		
		QiYeTextMsg text=new QiYeTextMsg();
		text.setAgentid("16");
		text.setText(new Text("测试消息"));
		text.setSafe("1");
		text.setTouser("@all");
		ApiResult sendTextMsg = SendMessageApi.sendTextMsg(text);
		renderText(sendTextMsg.getJson());
	}

/**
	 * 图文混排的消息
	 */
	public void sendNewsMessage(){
		QiYeNewsMsg qiYeNewsMsg=new QiYeNewsMsg();
		qiYeNewsMsg.setAgentid("16");
		qiYeNewsMsg.setArticleCount(1);
		qiYeNewsMsg.setSafe("0");
		qiYeNewsMsg.setTouser("@all");
		
		News news=new News();
		List<Article> articles=new ArrayList<Article>();
		Article article=new Article();
		article.setTitle("微信开发源码");
		article.setDescription("微信公众号开发源码共享");
		article.setPicurl("https://mmbiz.qlogo.cn/mmbiz/ibHRiaZ9MRcUpjHhhNQzCl9zGicPBWibh1ndW6Mj27ibCREGGVa9mag0iatwDJ1fSPhsib2LiaBVVenAU8ibqW1kGeka9HQ/0?wx_fmt=png");
		article.setUrl("http://mp.weixin.qq.com/s?__biz=MzA4MDA2OTA0Mg==&mid=400919708&idx=1&sn=c35cf7fe2c77f19f4c3edcdb9607925f#rd");
		articles.add(article);
		news.setArticles(articles);
		qiYeNewsMsg.setNews(news);
		
		ApiResult sendTextMsg = SendMessageApi.sendNewsMsg(qiYeNewsMsg);
		renderText(sendTextMsg.getJson());
	}

/**
	 * 发送图片
	 */
	public void sendImage(){
		QiYeImageMsg image=new QiYeImageMsg();
		image.setAgentid("16");
		image.setSafe("0");
		image.setTouser("Javen");
		image.setMedia_id("1s5aCsU1-sPwQMcf_exgRGliqQ6RMwpNv0sHm6J-e1ICIQdC6q4rTX72Ob-0hqgITdDK3twbFtJ_XRilP9zWvhg");
		ApiResult apiResult = SendMessageApi.sendImageMsg(image);
		renderText(apiResult.getJson());
	}