您的位置:首页 > 移动开发 > 微信开发

简谈微信公众服务号的客服接口

2015-12-02 07:40 447 查看
最近可能因业务需要增加了消息主动推送功能,可是服务号的模板推送数量限制的可怜,后来想到用客服推送简单有效。废话少说,直接贴部分作为日后再次开发的依据和记录。

#region AccessToken类
public class accessToken
{
public string access_token { get; set; }
public string expires_in { get; set; }
}
#endregion


#region 获取Access访问令牌
public string[] GetFWHAccessToken()
{

string sToken = "xxxx";
string sAppID = "wx5edxxxx698781866";
string sAppSecret = "6e7xxxxb7a999d";
string[] res = null; res = new string[2];

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + sAppID + "&secret=" + sAppSecret);
// https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=id&corpsecret=secrect //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + sAppID + "&corpsecret=" + sAppSecret + "");
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
accessToken deserializedProduct = (accessToken)JsonConvert.DeserializeObject(content, typeof(accessToken));
res[0] = deserializedProduct.access_token;
res[1] = deserializedProduct.expires_in;
}
return res;
}
#endregion


public string GetFWHAccessTokenExpire()
{

string sResult = "";

try
{
DateTime dtLastTime = DateTime.Now;
int iExpireValue = 100;

DataTable dtSysParam = HcDb.Query("Select * from WX_Param Where PARAMNAME in ('ACCESSTOKENFWH','ACCESSTOKENEXPFWH','ACCESSTOKENLASTTIMEFWH')").Tables[0];
if (dtSysParam != null && dtSysParam.Rows.Count > 0)
{
foreach (DataRow drParam in dtSysParam.Rows)
{
if (drParam["PARAMNAME"].ToString() == "ACCESSTOKENLASTTIMEFWH")
{
dtLastTime = HcVal.StrToDateTime(drParam["PARAMVALUE"].ToString());
}
else if (drParam["PARAMNAME"].ToString() == "ACCESSTOKENEXPFWH")
{
iExpireValue = HcVal.StrToInt(drParam["PARAMVALUE"].ToString());
}
else if (drParam["PARAMNAME"].ToString() == "ACCESSTOKENFWH")
{
sResult = drParam["PARAMVALUE"].ToString();
}
}
if (dtLastTime.AddSeconds(iExpireValue) <= DateTime.Now)
{
string[] arrAccessToken = GetFWHAccessToken();
ArrayList sSqls = new ArrayList();
string sSql = "Update WX_PARAM Set PARAMVALUE='" + arrAccessToken[0] + "' where PARAMNAME='ACCESSTOKENFWH'";
sSqls.Add(sSql);
sSql = "Update WX_PARAM Set PARAMVALUE='" + arrAccessToken[1] + "' where PARAMNAME='ACCESSTOKENEXPFWH'";
sSqls.Add(sSql);
sSql = "Update WX_PARAM Set PARAMVALUE='" + DateTime.Now.ToString() + "' where PARAMNAME='ACCESSTOKENLASTTIMEFWH'";
sSqls.Add(sSql);
HcDb.ExecuteSqlTran(sSqls);
sResult = arrAccessToken[0];

}
}

}
catch
{

}
return sResult;
}


#region 服务号客服推送信息
/// <summary>
/// 推送信息
/// </summary>
/// <param name="corpid">企业号ID</param>
/// <param name="corpsecret">管理组密钥</param>
/// <param name="paramData">提交的数据json</param>
/// <param name="sAccessToken">AccessToken</param>
/// <returns></returns>
public string SendKFMessage(string paramData, string sAccessToken)
{
string postUrl = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", sAccessToken);
return PostWebRequest(postUrl, paramData, Encoding.UTF8);

}

#endregion


public bool PullKFText(string sContent, string sToUSerName)
{
bool bResult = false;
if (string.IsNullOrWhiteSpace(sToUSerName))
{
return bResult;
}

//发送消息,把收到的内容回复给发送者
string sResult = "{";
sResult += "\"touser\": \"" + sToUSerName + "\",";
sResult += "\"msgtype\": \"text\",";
sResult += "\"text\": {";
sResult += "  \"content\": \"" + sContent + "\"";
sResult += "},";
sResult += "\"safe\":\"0\"";
sResult += "}";

if (SendKFMessage(sResult, GetFWHAccessTokenExpire()).IndexOf("ok") > 0)
{
bResult = true;
}

return bResult;
}


//调用如此简单。。。
PullKFText(sPullMsg, sToUser)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: