企業(yè)號升級到企業(yè)微信后茅逮,發(fā)送應(yīng)用消息的接口也變化了不少璃赡,除了原來的文本、圖片献雅、文件碉考、語音、視頻挺身、圖文消息等消息外侯谁,增加了文本卡片、markdown消息章钾、小程序通知消息等內(nèi)容良蒸,不過它們都可以共用一個接口進(jìn)行發(fā)送,只是它們的對象不太一樣伍玖,本篇隨筆主要介紹整個企業(yè)微信應(yīng)用消息處理這部分內(nèi)容嫩痰,包括不同消息的實(shí)體關(guān)系和接口發(fā)送的實(shí)現(xiàn)等內(nèi)容。
1窍箍、企業(yè)微信消息對象的定義
在早期還是微信企業(yè)號的時候串纺,我對發(fā)送企業(yè)號消息的接口也已經(jīng)實(shí)現(xiàn),參考《C#開發(fā)微信門戶及應(yīng)用(19)-微信企業(yè)號的消息發(fā)送(文本椰棘、圖片纺棺、文件、語音邪狞、視頻祷蝌、圖文消息等)》,這次對企業(yè)號升級到企業(yè)微信接口帆卓,對所有接口進(jìn)行了梳理和測試巨朦。
我們先看看企業(yè)微信對應(yīng)用消息接口的介紹(https://work.weixin.qq.com/api/doc#90000/90135/90236)
根據(jù)消息的類型,我們增加了一些額外的對象實(shí)體類剑令,修改后的關(guān)系圖如下所示糊啡。
我們來看看新增的文本卡片、markdown消息吁津、小程序通知消息等內(nèi)容的對象定義代碼棚蓄。
所有消息的基類信息CorpSendBase基類對象代碼如下
/// <summary>
/// 企業(yè)號發(fā)送消息的基礎(chǔ)消息內(nèi)容
/// </summary>
public class CorpSendBase
{
/// <summary>
/// 成員ID列表(消息接收者,多個接收者用‘|’分隔碍脏,最多支持1000個)梭依。特殊情況:指定為@all,則向關(guān)注該企業(yè)應(yīng)用的全部成員發(fā)送
/// </summary>
public string touser { get; set; }
/// <summary>
/// 部門ID列表典尾,多個接收者用‘|’分隔役拴,最多支持100個。當(dāng)touser為@all時忽略本參數(shù)
/// </summary>
public string toparty { get; set; }
/// <summary>
/// 標(biāo)簽ID列表急黎,多個接收者用‘|’分隔扎狱,最多支持100個。當(dāng)touser為@all時忽略本參數(shù)
/// </summary>
public string totag { get; set; }
/// <summary>
/// 消息類型
/// </summary>
public string msgtype { get; set; }
/// <summary>
/// 企業(yè)應(yīng)用的id勃教,整型淤击。可在應(yīng)用的設(shè)置頁面查看
/// </summary>
public string agentid { get; set; }
/// <summary>
/// 表示是否是保密消息故源,0表示否污抬,1表示是,默認(rèn)0
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string safe { get; set; }
}
其他的文本卡片绳军、markdown消息印机、小程序通知消息等內(nèi)容的對象繼承它,增加自己對象的消息即可门驾。
如文本卡片的類及其子類的代碼如下所示射赛。
/// <summary>
/// 文本卡片消息
/// </summary>
public class CorpSendTextCard : CorpSendBase
{
public CorpSendTextCard()
{
this.msgtype = CorpMsgType.textcard;
this.textcard = new CorpSendTextCardEntity();
}
public CorpSendTextCard(string title, string description, string url, string btntxt = "詳情")
{
this.msgtype = CorpMsgType.textcard;
this.textcard = new CorpSendTextCardEntity(title, description, url, btntxt);
}
/// <summary>
/// 消息內(nèi)容
/// </summary>
public CorpSendTextCardEntity textcard { get; set; }
}
public class CorpSendTextCardEntity
{
/// <summary>
/// 標(biāo)題,不超過128個字節(jié)奶是,超過會自動截斷
/// </summary>
public string title { get; set; }
/// <summary>
/// 描述楣责,不超過512個字節(jié),超過會自動截斷
/// </summary>
public string description { get; set; }
/// <summary>
/// 點(diǎn)擊后跳轉(zhuǎn)的鏈接聂沙。
/// </summary>
public string url { get; set; }
/// <summary>
/// 按鈕文字秆麸。 默認(rèn)為“詳情”, 不超過4個文字及汉,超過自動截斷沮趣。
/// </summary>
public string btntxt { get; set; }
public CorpSendTextCardEntity()
{ }
public CorpSendTextCardEntity(string title, string description, string url, string btntxt = "詳情")
{
this.title = title;
this.description = description;
this.url = url;
this.btntxt = btntxt;
}
}
效果借用官方的效果圖,如下所示
markdown消息對象如下所示
/// <summary>
/// markdown消息
/// 目前僅支持markdown語法的子集
/// 微工作臺(原企業(yè)號)不支持展示markdown消息
/// </summary>
public class CorpSendMarkdown : CorpSendBase
{
public CorpSendMarkdown()
{
this.msgtype = CorpMsgType.markdown;
this.markdown = new CorpSendMarkdownEntity();
}
public CorpSendMarkdown(string content)
{
this.msgtype = CorpMsgType.markdown;
this.markdown = new CorpSendMarkdownEntity(content);
}
/// <summary>
/// 消息內(nèi)容
/// </summary>
public CorpSendMarkdownEntity markdown { get; set; }
}
效果如下所示
小程序通知消息
/// <summary>
/// 小程序通知消息
/// </summary>
public class CorpSendMiniProgram : CorpSendBase
{
public CorpSendMiniProgram()
{
this.msgtype = CorpMsgType.miniprogram_notice;
this.textcard = new CorpSendMiniProgramEntity();
}
/// <summary>
/// 消息內(nèi)容
/// </summary>
public CorpSendMiniProgramEntity textcard { get; set; }
}
小程序的消息界面效果如下
有了這些消息的定義坷随,我們就可以統(tǒng)一使用接口進(jìn)行發(fā)送了房铭。
2、發(fā)送企業(yè)微信信息
定義一個消息發(fā)送的接口温眉,接口函數(shù)的參數(shù)育叁,包括accesstoken和消息對象的基類,如下所示芍殖。
/// <summary>
/// 企業(yè)微信消息管理接口定義
/// </summary>
public interface ICorpMessageApi
{
/// <summary>
/// 發(fā)送消息豪嗽。
/// 消息型應(yīng)用支持文本、圖片豌骏、語音龟梦、視頻、文件窃躲、圖文等消息類型计贰。主頁型應(yīng)用只支持文本消息類型,且文本長度不超過20個字蒂窒。
/// 需要管理員對應(yīng)用有使用權(quán)限躁倒,對收件人touser荞怒、toparty、totag有查看權(quán)限秧秉,否則本次調(diào)用失敗褐桌。
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
CommonResult SendMessage(string accessToken, CorpSendBase data);
}
實(shí)現(xiàn)接口的代碼如下所示
/// <summary>
/// 發(fā)送消息。
/// 消息型應(yīng)用支持文本象迎、圖片荧嵌、語音、視頻砾淌、文件啦撮、圖文等消息類型。主頁型應(yīng)用只支持文本消息類型汪厨,且文本長度不超過20個字赃春。
/// 需要管理員對應(yīng)用有使用權(quán)限,對收件人touser劫乱、toparty聘鳞、totag有查看權(quán)限,否則本次調(diào)用失敗要拂。
/// </summary>
/// <param name="accessToken"></param>
/// <returns></returns>
public CommonResult SendMessage(string accessToken, CorpSendBase data)
{
CommonResult result = new CommonResult();
var url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", accessToken);
var postData = data.ToJson();
//數(shù)據(jù)不用加密發(fā)送
CorpSendResult sendResult = WeJsonHelper<CorpSendResult>.ConvertJson(url, postData);
if (sendResult != null)
{
result.Success = (sendResult.errcode == 0);
if (!result.Success)
{
result.ErrorMessage = string.Format("invaliduser:{0},invalidparty:{1},invalidtag:{2}",
sendResult.invaliduser, sendResult.invalidparty, sendResult.invalidtag);
}
}
return result;
}
定義好相應(yīng)的發(fā)送對象后抠璃,我們就可以進(jìn)行統(tǒng)一的消息發(fā)送操作,包括文本脱惰、圖片搏嗡、文件、語音等等類型的消息拉一,注意有些消息是需要上傳到服務(wù)器上采盒,然后在根據(jù)mediaId進(jìn)行發(fā)送出去的。
程序測試接口發(fā)送的調(diào)用代碼如下所示:
文本卡片發(fā)送代碼
/// <summary>
/// 文本卡片消息發(fā)送
/// </summary>
private void btnSendTextCard_Click(object sender, EventArgs e)
{
ICorpMessageApi bll = new CorpMessageApi();
CorpSendTextCard msg = new CorpSendTextCard("中秋節(jié)禮品領(lǐng)取", "今年中秋節(jié)公司有豪禮相送", "http://www.iqidi.com", "更多詳情");
msg.agentid = agentid;
msg.touser = "wuhuacong";
CommonResult result = bll.SendMessage(token, msg);
if (result != null)
{
Console.WriteLine("發(fā)送TextCard消息:{0} {1} {2}", fileMediaId, (result.Success ? "成功" : "失敗"), result.ErrorMessage);
}
}
Markdown的發(fā)送代碼如下所示蔚润。
/// <summary>
/// MarkDown消息發(fā)送
/// </summary>
private void btnSendMarkDown_Click(object sender, EventArgs e)
{
ICorpMessageApi bll = new CorpMessageApi();
string content = @"您的會議室已經(jīng)預(yù)定磅氨,稍后會同步到`郵箱`
>**事項詳情**
>事 項:<font color='info'>開會</font>
>組織者:@miglioguan
>參與者:@miglioguan、@kunliu嫡纠、@jamdeezhou烦租、@kanexiong、@kisonwang
>
>會議室:< font color ='info'>廣州TIT 1樓 301</font>
>日期:< font color ='warning'>2018年5月18日</font>
>時間:< font color ='comment'>上午9:00-11:00</font>
>
> 請準(zhǔn)時參加會議除盏。
>
> 如需修改會議信息叉橱,請點(diǎn)擊:[修改會議信息] (https://work.weixin.qq.com)";
CorpSendMarkdown msg = new CorpSendMarkdown(content);
msg.agentid = agentid;
msg.touser = "wuhuacong";
CommonResult result = bll.SendMessage(token, msg);
if (result != null)
{
Console.WriteLine("發(fā)送Markdown消息:{0} {1} {2}", fileMediaId, (result.Success ? "成功" : "失敗"), result.ErrorMessage);
}
}