この記事では主に、asp.net WeChat 開発における高度な大量テキスト メッセージングの関連コンテンツを紹介します。必要な方は參考にしてください。
まず、私の個(gè)人的な開発プログラムにおける大量テキスト メッセージングのプロセスを説明します。最初に UI が必要です。その後、コードの作成を開始できます。インターフェイスは次のとおりです。
図を見ると、最初に WeChat ID とメッセージの數(shù)を取得する必要があることがわかります。計(jì)算方法としては、1 つのメッセージをローカル データベースに保存してアイテム數(shù)を計(jì)算するだけです (これは可能だと思います)。 4 つを超えるアイテムが送信されると、送信できません (サービス アカウントは月に 4 つのアイテムしか送信できず、それ以上送信する必要がないため、ここで制限しています。プレビューを使用しない限り、ユーザーは 4 つのメッセージしか受信できません)ただし、プレビュー機(jī)能は 100 回しか送信できません。その後、WeChat パブリック プラットフォームのバックエンドに入ったので、開発者モードを使用してグループ メッセージを N 回送信できるかもしれません。公式ウェブサイトで実際にグループで 4 つのメッセージを送信できることを確認(rèn)しましたが、これは少し憂鬱でした。) グループ メッセージの數(shù)を節(jié)約するために、ターゲット グループはすべてのユーザーまたはグループ化されたユーザーとして選択できます。ここにグループ テキストが表示されます。詳細(xì)については、次のコードを參照してください:
今月のグループ メッセージの殘りの數(shù)をバインドする
グループ リストをバインドする
/// <summary>
/// 綁定本月剩余群發(fā)條數(shù)
/// </summary>
private void BindMassCount()
{
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
//官方微信服務(wù)號(hào)每月只能群發(fā)4條信息,(訂閱號(hào)每天1條)多余信息,將不會(huì)成功推送,這里已經(jīng)設(shè)定為4
this.lbMassCounts.Text = (4 - int.Parse(wxmaslist.Count.ToString())).ToString();
if (wxmaslist.Count >= 4)
{
this.LinkBtnSubSend.Enabled = false;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('群發(fā)信息已達(dá)上限!請(qǐng)下月初再試!')");
}
else
{
this.LinkBtnSubSend.Enabled = true;
this.LinkBtnSubSend.Attributes.Add("Onclick", "return confirm('您確定要群發(fā)此條信息??')");
}
}
/// <summary> /// 綁定分組列表 /// </summary> private void BindGroupList() { WeiXinServer wxs = new WeiXinServer(); ///從緩存讀取accesstoken string Access_token = Cache["Access_token"] as string; if (Access_token == null) { //如果為空,重新獲取 Access_token = wxs.GetAccessToken(); //設(shè)置緩存的數(shù)據(jù)7000秒后過期 Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } string Access_tokento = Access_token.Substring(17, Access_token.Length - 37); string jsonres = ""; string content = Cache["AllGroups_content"] as string; if (content == null) { jsonres = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=" + Access_tokento; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(jsonres); myRequest.Method = "GET"; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); content = reader.ReadToEnd(); reader.Close(); //設(shè)置緩存的數(shù)據(jù)7000秒后過期 Cache.Insert("AllGroups_content", content, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration); } //使用前需要引用Newtonsoft.json.dll文件 JObject jsonObj = JObject.Parse(content); int groupsnum = jsonObj["groups"].Count(); this.DDLGroupList.Items.Clear();//清除 for (int i = 0; i < groupsnum; i++) { this.DDLGroupList.Items.Add(new ListItem(jsonObj["groups"][i]["name"].ToString() + "(" + jsonObj["groups"][i]["count"].ToString() + ")", jsonObj["groups"][i]["id"].ToString())); } } /// <summary> /// 選擇群發(fā)對(duì)象類型,顯示隱藏分組列表項(xiàng) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DDLMassType_SelectedIndexChanged(object sender, EventArgs e) { if (int.Parse(this.DDLMassType.SelectedValue.ToString()) > 0) { this.DDLGroupList.Visible = true; } else { this.DDLGroupList.Visible = false; } }
送信前にプレビュー
/// <summary>
/// 群發(fā)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkBtnSubSend_Click(object sender, EventArgs e)
{
//根據(jù)單選按鈕判斷類型,發(fā)送
///如果選擇的是文本消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("0"))
{
if (String.IsNullOrWhiteSpace(this.txtwenben.InnerText.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)輸入您要群發(fā)文本內(nèi)容!');", true);
return;
}
if (this.txtwenben.InnerText.ToString().Trim().Length<10)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('文本內(nèi)容至少需要10個(gè)字符以上!');", true);
return;
}
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
if (wxmaslist.Count >= 4)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群發(fā)消息數(shù)量已達(dá)上限!');", true);
return;
}
else
{
//如何群發(fā)類型為全部用戶,根據(jù)openID列表群發(fā)給全部用戶,訂閱號(hào)不可用,服務(wù)號(hào)認(rèn)證后可用
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
StringBuilder sbs = new StringBuilder();
sbs.Append(GetAllUserOpenIDList());
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "touser":[
// "OPENID1",
// "OPENID2"
// ],
// "msgtype": "text",
// "text": { "content": "hello from boxer."}
//}
string postData = "{\"touser\":[" + sbs.ToString() +
"],\"msgtype\":\"text\",\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
"\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
//群發(fā)成功后,保存記錄
WxMassInfo wmi = new WxMassInfo();
wmi.ImageUrl = "";
wmi.type = "文本";
wmi.contents = this.txtwenben.InnerText.ToString().Trim();
wmi.title = this.txtwenben.InnerText.ToString().Substring(0, 10) + "...";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。。?shù)據(jù)保存失?。?amp;#39;);", true);
return;
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。。?amp;#39;);", true);
return;
}
}
else
{
string group_id = this.DDLGroupList.SelectedValue.ToString();
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "filter":{
// "is_to_all":false
// "group_id":"2"
// },
// "text":{
// "content":"CONTENT"
// },
// "msgtype":"text"
//}
//}
string postData = "{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\"" + group_id +
"\"},\"text\":{\"content\":\"" + this.txtwenben.InnerText.ToString() +
"\"},\"msgtype\":\"text\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
//群發(fā)成功后,保存記錄
WxMassInfo wmi = new WxMassInfo();
wmi.ImageUrl = "";
wmi.type = "文本";
wmi.contents = this.txtwenben.InnerText.ToString().Trim();
wmi.title = this.txtwenben.InnerText.ToString().Substring(0, 10) + "...";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。。?shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。。?shù)據(jù)保存失?。?amp;#39;);", true);
return;
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。?!');", true);
return;
}
}
}
}
//如果選擇的是圖文消息
if (this.RadioBtnList.SelectedValue.ToString().Equals("1"))
{
if (String.IsNullOrWhiteSpace(this.lbtuwenmedai_id.Text.ToString().Trim()))
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('請(qǐng)選擇或新建圖文素材再進(jìn)行群發(fā)!');", true);
return;
}
WxMassService wms = new WxMassService();
List<WxMassInfo> wxmaslist = wms.GetMonthMassCount();
if (wxmaslist.Count >= 4)
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('本月可群發(fā)消息數(shù)量已達(dá)上限!');", true);
return;
}
else
{
//如何群發(fā)類型為全部用戶,根據(jù)openID列表群發(fā)給全部用戶,訂閱號(hào)不可用,服務(wù)號(hào)認(rèn)證后可用
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
StringBuilder sbs = new StringBuilder();
sbs.Append(GetAllUserOpenIDList());
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "touser":[
// "OPENID1",
// "OPENID2"
// ],
// "mpnews":{
// "media_id":"123dsdajkasd231jhksad"
// },
// "msgtype":"mpnews"
//}
string postData = "{\"touser\":[" + sbs.ToString() +
"],\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
"\"},\"msgtype\":\"mpnews\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
Session["media_id"] = null;
WxMassInfo wmi = new WxMassInfo();
if (Session["wmninfo"] != null)
{
WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;
wmi.title = wmninfo.title.ToString();
wmi.contents = wmninfo.contents.ToString();
wmi.ImageUrl = wmninfo.ImageUrl.ToString();
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失??!');", true);
return;
}
}
else
{
wmi.title = "";
wmi.contents = "";
wmi.ImageUrl = "";
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功!??!圖文部分?jǐn)?shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失敗!');", true);
return;
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。?!');", true);
return;
}
}
else
{
//根據(jù)分組進(jìn)行群發(fā),訂閱號(hào)和服務(wù)號(hào)認(rèn)證后均可用
string group_id = this.DDLGroupList.SelectedValue.ToString();
WeiXinServer wxs = new WeiXinServer();
///從緩存讀取accesstoken
string Access_token = Cache["Access_token"] as string;
if (Access_token == null)
{
//如果為空,重新獲取
Access_token = wxs.GetAccessToken();
//設(shè)置緩存的數(shù)據(jù)7000秒后過期
Cache.Insert("Access_token", Access_token, null, DateTime.Now.AddSeconds(7000), System.Web.Caching.Cache.NoSlidingExpiration);
}
string Access_tokento = Access_token.Substring(17, Access_token.Length - 37);
string posturl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + Access_tokento;
///群發(fā)POST數(shù)據(jù)示例如下:
// {
// "filter":{
// "is_to_all":false
// "group_id":"2"
// },
// "mpnews":{
// "media_id":"123dsdajkasd231jhksad"
// },
// "msgtype":"mpnews"
//}
string postData = "{\"filter\":{\"is_to_all\":\"false\"\"group_id\":\""+group_id+
"\"},\"mpnews\":{\"media_id\":\"" + this.lbtuwenmedai_id.Text.ToString() +
"\"},\"msgtype\":\"mpnews\"}";
string tuwenres = wxs.GetPage(posturl, postData);
//使用前需藥引用Newtonsoft.json.dll文件
JObject jsonObj = JObject.Parse(tuwenres);
if (jsonObj["errcode"].ToString().Equals("0"))
{
Session["media_id"] = null;
WxMassInfo wmi = new WxMassInfo();
if (Session["wmninfo"] != null)
{
WxMpNewsInfo wmninfo = Session["wmninfo"] as WxMpNewsInfo;
wmi.title = wmninfo.title.ToString();
wmi.contents = wmninfo.contents.ToString();
wmi.ImageUrl = wmninfo.ImageUrl.ToString();
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。。?shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!數(shù)據(jù)保存失敗!');", true);
return;
}
}
else
{
wmi.title = "";
wmi.contents = "";
wmi.ImageUrl = "";
wmi.type = "圖文";
if (this.DDLMassType.SelectedValue.ToString().Equals("0"))
{
wmi.massObject = this.DDLMassType.SelectedItem.Text.ToString();
}
else
{
wmi.massObject = this.DDLGroupList.SelectedItem.Text.ToString();
}
wmi.massStatus = "成功";//群發(fā)成功之后返回的狀態(tài)碼
wmi.massMessageID = jsonObj["msg_id"].ToString();//群發(fā)成功之后返回的消息ID
wmi.massDate = System.DateTime.Now.ToString();
int num = wms.AddWxMassInfo(wmi);
if (num > 0)
{
Session["wmninfo"] = null;
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功?。?!圖文部分?jǐn)?shù)據(jù)已保存!');location='WxMassManage.aspx';", true);
return;
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)已提交成功!??!數(shù)據(jù)保存失敗!');", true);
return;
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "", "alert('群發(fā)任務(wù)提交失?。。?amp;#39;);", true);
return;
}
}
}
}
}
重要な部分は、すべてのユーザーの openID を取得し、それらを文字列に連結(jié)することです。
れぇこれで終わります, 次の章では、引き続きグラフィック情報(bào)のグループ送信について説明します。グループにグラフィック情報(bào)を送信する前に、グラフィック情報(bào)に必要な素材をアップロードし、media_id を取得する必要があるため、この章では紹介しません。単一のグラフィックメッセージを作成してグループに送信する方法を紹介します。皆さんに気に入っていただければ幸いです。
asp.net WeChat 開発 (高度なマス テキスト) 関連記事をさらに詳しく知りたい場(chǎng)合は、PHP 中國(guó)語 Web サイトに注目してください。

ホットAIツール

Undress AI Tool
脫衣畫像を無料で

Undresser.AI Undress
リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover
寫真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡(jiǎn)単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中國(guó)語版
中國(guó)語版、とても使いやすい

ゼンドスタジオ 13.0.1
強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)