微信小程序> 小程序客服消息

小程序客服消息

浏览量:1491 时间: 来源:lmzj

前几天需要做一个小程序后台自动回复功能。没怎么接触过,遇到了不少坑,所以总结一下。

思路大概是,用户在小程序中点击客服按钮后。微信服务器会发送get请求到我们在微信平台配置好的服务器地址验证。然后根据用户的消息发送对应的数据包,数据格式也是自己配置好的。

后台拿到用户的请求数据后,按照自己的业务逻辑处理。然后请求微信的发送客服消息给用户接口,根据业务逻辑,下发不同类型的消息给微信服务器,然后微信服务器就会返回给用户。

40003不合法的 OpenID,请开发者确认 OpenID 是否是其他小程序的 OpenID

遇到40003的问题。如果确定openid获取正确,则是因为传参时,openid没有带上" "所致。调用这个接口传的参数是要以json格式来传的。以文本类型的参数为例:

@Testpublic void test_111() {    String access_token = "access_token";    String open_id = "open_id";    String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token;    MapString, String param = new HashMap();    param.put("access_token", access_token);    param.put("touser", open_id);    param.put("msgtype", "text");    param.put("content", "hello world");    String json = JSON.toJSONString(param);    String result = sendPost(url, json);    System.out.println("result=" + result);}public String sendPost(String url, String data) {        String response = null;        try {            CloseableHttpClient httpclient = null;            CloseableHttpResponse httpresponse = null;            try {                httpclient = HttpClients.createDefault();                HttpPost httpPost = new HttpPost(url);                RequestConfig requestConfig = RequestConfig.custom()                        .setSocketTimeout(4000).setConnectTimeout(4000).build();                httpPost.setConfig(requestConfig);                httpPost.setHeader("Content-Type", "application/json;charset=utf-8");                 StringEntity stringentity = new StringEntity(data, "utf-8");                stringentity.setContentEncoding("utf-8");                httpPost.setEntity(stringentity);                httpresponse = httpclient.execute(httpPost);                response = EntityUtils.toString(httpresponse.getEntity());            } finally {                if (httpclient != null) {                    httpclient.close();                }                if (httpresponse != null) {                    httpresponse.close();                }            }        } catch (Exception e) {            e.printStackTrace();        }        return response;    }

如果参数是通过字符串拼接,需要对参数进行一次json转换,否则在下发文本类型的消息时,content内容有中文时,会遇到44004的错误。

Object obj = JSONValue.parse(param_);String json = obj.toString();

 

 

 

 

 

 

 

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎