企业之发送图片消息(源码下载)
企业之发送图⽚消息(源码下载)
前⾔:
本篇⽂章我将围绕如何实现应⽤发送图⽚消息给⽤户,
发送⽅:⼀个⾃建应⽤  接收⽅:企业⽤户。
准备内容:
1.如果没有⾃建应⽤的话,需要在企业管理后台建⽴⼀个⾃建应⽤。
2.准备企业的公司id 和 应⽤的AgentID 和 AgentSecret,这些都可以在企业管理后台看到。
3.在本地准备⼀张图⽚ 供上传使⽤。
发送图⽚功能逻辑:
毛阿敏的老公是谁
点击图⽚上传功能,将需要发送的图⽚通过调⽤上传⽂件素材接⼝存到企业服务器上,然后调⽤企业下载⽂件素材接⼝得到该图⽚的media_id ,接下来就可以组装发送图⽚消息的代码,发送请求,图⽚发送成功!
准备开始:
1.TestController
st.controller;
st.service.WeiMessageService;
st.wechat.SendApplicationMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by tanghh on 2019/9/1
*/
@RestController
public class TestController {
@Autowired
private WeiMessageService weiMessageService;
@GetMapping(value = "/testSendNews")
public void testSendNews(){
//1.发送⽂本消息
//        SendApplicationMessage message = new SendApplicationMessage();
中国奶粉品牌排行榜//        stSendText();
//2.发送图⽚消息
weiMessageService.sendImageMessage("D:/project/csdn/有钱.gif","image");    }
}
2.Service
st.service;
/**
* @Author tanghh
* @Date 2020/6/29 15:41
*/
public interface WeiMessageService {
/**
* 发送图⽚消息
* @param filePath
* @param type
*/
void sendImageMessage(String filePath,String type);
}
st.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
st.model.Image;
st.model.ImageMessage;
st.util.QiWeiParametersUtil;
st.util.QiYeWeiUtil;
st.wechat.SendRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.File;
/**
* @Author tanghh
* @Date 2020/6/29 15:41
*/
@Service
public class WeiMessageServiceImpl implements WeiMessageService {
private Logger logger = Logger(WeiMessageServiceImpl.class);
@Override
public void sendImageMessage(String filePath, String type) {
//1.将获取到的图⽚上传到企业服务器
JSONObject resultJsonObject = uploadTempMaterial(filePath,"image");
中国县域人口密度榜出炉
String mediaId = String("media_id");
//将图⽚发给⽤户(企业或者是) ⽤户如果在24⼩时之内没有与公众平台交互,⽤户将接收不到的消息        String accessToken = AccessToken();
//2.组装图⽚消息
//2.1.创建图⽚消息对象
ImageMessage sendImgMessage = new ImageMessage();
//2.2⾮必需发送⽤户id
sendImgMessage.setTouser("TangHuiHong");
//2.3 发送类型
sendImgMessage.setMsgtype(type);
sendImgMessage.setAgentid(QiWeiParametersUtil.agentId);
Image image = new Image();
大连医科大学是几本image.setMedia_id(mediaId);
sendImgMessage.setImage(image);
//2.获取请求的url
String sendMessage_url = place("ACCESS_TOKEN", accessToken);
String json = JSONString(sendImgMessage);
//3.发送消息:调⽤业务类,发送消息
JSONObject jsonObject = SendRequest.sendPost(sendMessage_url, json);
JSONObject jsonObject = SendRequest.sendPost(sendMessage_url, json);
//4.错误消息处理
if (null != jsonObject) {
if (0 != IntValue("errcode")) {
<("发送消息失败 errcode:{} errmsg:{}", IntValue("errcode"), String("errmsg"));            }
}
}
/**
* 企业上传临时素材
* ⽂件上传并获取上传⽂件的mediaId
* type  媒体⽂件类型,分别有图⽚(image)、语⾳(voice)、视频(video),普通⽂件(file)
*
* @param
* @return
*/
public JSONObject uploadTempMaterial(String filePath, String type) {
//1.创建本地⽂件
File file = new File(filePath);
String accessToken = AccessToken();
//2.拼接请求url
String uploadTempMaterialUrl = place("ACCESS_TOKEN", accessToken)
.replace("TYPE", type);
//3.调⽤接⼝,发送请求,上传⽂件到服务器
String result = QiYeWeiUtil.httpRequest(uploadTempMaterialUrl, file);
//4.json字符串转对象:解析返回值,json反序列化
result = placeAll("[\\\\]", "");
JSONObject resultJSON = JSONObject.parseObject(result);
//5.返回参数判断
if (resultJSON != null) {
if (("media_id") != null) {
System.out.println("上传" + type + "永久素材成功");
return resultJSON;
} else {
System.out.println("上传" + type + "永久素材失败");
}
}
return resultJSON;
}
}
3.涉及的实体类
st.model;
import lombok.Data;
/
**
* Created by tanghh on 2019/8/30
*/
斛怎么读public @Data
class ImageMessage {
private String touser;
//    private String toparty;
//    private String totag;
private String msgtype;
private Integer agentid;
private Image image;
private Integer safe;
林俊杰金曲奖
}
st.model;
import lombok.Data;
/**
* 图⽚消息
* @Author: tanghh18
* @Date: 2019/8/30 17:02
*/
public @Data
class Image {
private String media_id;
}
4.参数类
参数类中有些参数必须要替换掉 corpId  agentId  secret

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。