微信公众号关注后,可以通过客服消息向用户发送些欢迎、使用指南之类的。
发送客服消息
发送客服消息,只需要在用户关注之后执行即可,并不需要在公众号后台添加客服插件,也并不需要添加客服人员。public void SendText(string toUser, string text)
{
string s = @"{
""touser"": """ + toUser + @""",
""msgtype"": ""text"",
""text"": {
""content"": """ + text.Replace("\"", "").Replace("\r\n", "\\n") + @"""
}
}";
string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + AccessToken;
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string result = wc.UploadString(url, s);
}
public void SendImage(string toUser, string mediaId)
{
string s = @"{
""touser"": """ + toUser + @""",
""msgtype"": ""image"",
""image"": {
""media_id"": """ + mediaId + @"""
}
}";
string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + AccessToken;
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string result = wc.UploadString(url, s);
}
可以看出很简单,相关知识点:
不过还有一个话题,就是图片的 media_id 如何获取 ?
这个图片可以用接口上传,但是我们为了简单,就人工在微信公众号后台上传了,反正这图片也不会经常变更。然后我们用下面的程序获取最近 10 条图片素材的 media_id,找出需要的 media_id 放到前面去即可。string url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + AccessToken;
string s = @"{
""type"":""image"",
""offset"":0,
""count"":10
}";
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string result = wc.UploadString(url, s);
当然操作这些,还需要微信公众号后台配置相关的 IP 等信息,不然没权限。