微信第三方平台开发经验总结(七):发送客服消息

来源:互联网 发布:淘宝店铺照片怎么弄 编辑:程序博客网 时间:2024/05/16 11:02
发送客服消息

当用户和公众号产生特定动作的交互时(具体动作列表请见下方说明),微信将会把消息数据推送给开发者,开发者可以在一段时间内(目前修改为48小时)调用客服接口,通过POST一个JSON数据包来发送消息给普通用户。

微信公众平台技术文档
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547

客服接口-发消息

接口调用请求说明

http请求方式: POSThttps://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

各消息类型所需的JSON数据包如下:

发送文本消息

{
    "touser":"OPENID",
    "msgtype":"text",
    "text":
    {
         "content":"Hello World"
    }
}


发送客服文本消息

@Override
public String sendServiceMsg(String authorizerAppid, String touser, String content) {
    JSONObject text = new JSONObject();
    text.accumulate("content",content);
    JSONObject json = new JSONObject();
    json.accumulate("touser",touser);
    json.accumulate("msgtype","text");
    json.accumulate("text",text);
    String url = ThirdPartyConfig.CUSTOM_SEND_URL.replace("{authorizer_access_token}",thirdPartyService.getAuthorizerToken(authorizerAppid));
    String retStr = HttpsUtil.postHtpps(url,json.toString());
    logger.info("ThirdPartyServiceImpl:sendText:retStr={}",retStr);
    return retStr;
}


thirdPartyService.getAuthorizerToken(authorizerAppid)
因为authorizer_access_token是有时限的,所以每次调用的时候都有可能需要重新到微信请求
这个时候就需要用到authorizer_appid

5、获取(刷新)授权公众号或小程序的接口调用凭据(令牌)

该API用于在授权方令牌(authorizer_access_token)失效时,可用刷新令牌(authorizer_refresh_token)获取新的令牌。请注意,此处token是2小时刷新一次,开发者需要自行进行token的缓存,避免token的获取次数达到每日的限定额度。缓存方法可以参考:http://mp.weixin.qq.com/wiki/2/88b2bf1265a707c031e51f26ca5e6512.html

接口调用请求说明

http请求方式: POST(请使用https协议)
https:// api.weixin.qq.com /cgi-bin/component/api_authorizer_token?component_access_token=xxxxx

POST数据示例:

{
"component_appid":"appid_value",
"authorizer_appid":"auth_appid_value",
"authorizer_refresh_token":"refresh_token_value",
}
   

请求参数说明
参数说明component_appid第三方平台appidauthorizer_appid授权方appidauthorizer_refresh_token授权方的刷新令牌,刷新令牌主要用于第三方平台获取和刷新已授权用户的access_token,只会在授权时刻提供,请妥善保存。一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌
返回结果示例

{
"authorizer_access_token": "aaUl5s6kAByLwgV0BhXNuIFFUqfrR8vTATsoSHukcIGqJgrc4KmMJ-JlKoC_-NKCLBvuU1cWPv4vDcLN8Z0pn5I45mpATruU0b51hzeT1f8", 
"expires_in": 7200, 
"authorizer_refresh_token": "BstnRqgTJBXb9N2aJq6L5hzfJwP406tpfahQeLNxX0w"
}
   

结果参数说明
参数说明authorizer_access_token授权方令牌expires_in有效期,为2小时authorizer_refresh_token刷新令牌




@Override
    public String getAuthorizerToken(String appid) {

        /**如果令牌未过期,直接返回*/
        if (authorizerDBService.existsAccessToken(appid)){
            return authorizerDBService.getAccessToken(appid);
        }

        String componentAccessToken = getComponentAccessToken();
        logger.info("ThirdPartyServiceImpl:getAuthorizerToken:componentAccessToken={}",componentAccessToken);

        /**替换Url中的{component_access_token}*/
        String url = ThirdPartyConfig.REFRESH_AUTHORIZER_TOKEN_URL.replace("{component_access_token}",componentAccessToken);
        logger.info("ThirdPartyServiceImpl:getAuthorizerToken:url={}",url);

        JSONObject json = new JSONObject();
        json.accumulate("component_appid",ThirdPartyConfig.APP_ID);
        json.accumulate("authorizer_appid",appid);
        json.accumulate("authorizer_refresh_token",authorizerDBService.getRefreshToken(appid));

        /**发送Https请求到微信*/
        String retStr = HttpsUtil.postHtpps(url,json.toString());
        logger.info("ThirdPartyServiceImpl:getPreAuthCode:retStr={}",retStr);
        JSONObject resultJson = JSONObject.fromObject(retStr);

        /**在返回结果中获取信息*/
        String authorizerAccessToken = resultJson.getString("authorizer_access_token");
        logger.info("queryAuth:authorizer_access_token={}",authorizerAccessToken);
        /**保存到redis中*/
        authorizerDBService.setexAccessToken(appid, authorizerAccessToken,ThirdPartyConfig.TWO_HOUR);

        return authorizerAccessToken;
    }


阅读全文
0 0