Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

丰富 Webhook 接口:SendTemplateCard, SendFile #2787

Merged
merged 11 commits into from
Feb 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
修改标识:lishewen - 20190706
修改描述:v3.5.8 丰富 Webhook 接口:SendImage

修改标识:mc7246 - 20230211
修改描述:丰富 Webhook 接口:SendTemplateCard, SendFile

----------------------------------------------------------------*/

/*
Expand All @@ -20,6 +23,7 @@
using Senparc.CO2NET.Helpers.Serializers;
using Senparc.NeuChar;
using Senparc.Weixin.Entities;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -58,6 +62,58 @@ public static WorkJsonResult SendText(string key, string content, string[] menti
JsonSetting jsonSetting = new JsonSetting(true);
return Senparc.Weixin.CommonAPIs.CommonJsonSend.Send<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting);
}

/// <summary>
/// 群机器人发送模版卡片
/// </summary>
/// <param name="key">机器人Key</param>
/// <param name="requestData">模板信息数据</param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static WorkJsonResult SendTemplateCard(string key, TemplateCardRequestData requestData, int timeOut = Config.TIME_OUT)
{
var data = new
{
msgtype = "template_card",
template_card = new
{
requestData.card_type,
requestData.source,
requestData.main_title,
requestData.emphasis_content,
requestData.quote_area,
requestData.sub_title_text,
requestData.horizontal_content_list,
requestData.jump_list,
requestData.card_action,
requestData.card_image,
requestData.vertical_content_list,
requestData.image_text_area

}
};
JsonSetting jsonSetting = new JsonSetting(true);
return Senparc.Weixin.CommonAPIs.CommonJsonSend.Send<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting);
}

/// <summary>
/// 群机器人发送文件信息
/// </summary>
/// <param name="key">机器人Key</param>
/// <param name="media_id">文件id,通过文件上传接口获取</param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static WorkJsonResult SendFile(string key, string media_id, int timeOut=Config.TIME_OUT)
{
var data = new
{
msgtype = "file",
media_id
};
JsonSetting jsonSetting = new JsonSetting(true);
return Senparc.Weixin.CommonAPIs.CommonJsonSend.Send<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting);
}

/// <summary>
/// 群机器人发送markdown信息
/// </summary>
Expand Down Expand Up @@ -138,6 +194,23 @@ public static WorkJsonResult SendNews(string key, WebhookNews data, int timeOut
JsonSetting jsonSetting = new JsonSetting(true);
return Senparc.Weixin.CommonAPIs.CommonJsonSend.Send<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting);
}

/// <summary>
/// 上传文件
/// </summary>
/// <param name="key"></param>
/// <param name="filepath"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static UploadMediaResult UploadMedia(string key, string filepath, int timeOut = Config.TIME_OUT)
{
var url = string.Format(Config.ApiWorkHost + "/cgi-bin/webhook/upload_media?key={0}&type=file", key);
var fileDictionary = new Dictionary<string, string>();
fileDictionary["name"] = "media";
fileDictionary["filename"] = filepath;

return Senparc.CO2NET.HttpUtility.Post.PostFileGetJson<UploadMediaResult>(CommonDI.CommonSP, url, null, fileDictionary, null, timeOut: timeOut);
}
#endregion

#region 异步方法
Expand Down Expand Up @@ -165,6 +238,57 @@ public static async Task<WorkJsonResult> SendTextAsync(string key, string conten
JsonSetting jsonSetting = new JsonSetting(true);
return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting).ConfigureAwait(false);
}

/// <summary>
/// 【异步方法】群机器人发送模版卡片
/// </summary>
/// <param name="key">机器人Key</param>
/// <param name="requestData">模板信息数据</param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static async Task<WorkJsonResult> SendTemplateCardAsync(string key, TemplateCardRequestData requestData, int timeOut = Config.TIME_OUT)
{
var data = new
{
msgtype = "template_card",
template_card = new
{
requestData.card_type,
requestData.source,
requestData.main_title,
requestData.emphasis_content,
requestData.quote_area,
requestData.sub_title_text,
requestData.horizontal_content_list,
requestData.jump_list,
requestData.card_action,
requestData.card_image,
requestData.vertical_content_list,
requestData.image_text_area
}
};
JsonSetting jsonSetting = new JsonSetting(true);
return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting).ConfigureAwait(false);
}

/// <summary>
/// 群机器人发送文件信息
/// </summary>
/// <param name="key">机器人Key</param>
/// <param name="media_id">文件id,通过文件上传接口获取</param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static async Task<WorkJsonResult> SendFileAsync(string key, string media_id, int timeOut = Config.TIME_OUT)
{
var data = new
{
msgtype = "file",
media_id
};
JsonSetting jsonSetting = new JsonSetting(true);
return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting).ConfigureAwait(false);
}

/// <summary>
/// 【异步方法】群机器人发送markdown信息
/// </summary>
Expand Down Expand Up @@ -247,6 +371,23 @@ public static async Task<WorkJsonResult> SendNewsAsync(string key, WebhookNews d
JsonSetting jsonSetting = new JsonSetting(true);
return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync<WorkJsonResult>(key, _urlFormat, data, CommonJsonSendType.POST, timeOut, jsonSetting: jsonSetting).ConfigureAwait(false);
}

/// <summary>
/// 上传文件
/// </summary>
/// <param name="key"></param>
/// <param name="filepath"></param>
/// <param name="timeOut"></param>
/// <returns></returns>
public static async Task<UploadMediaResult> UploadMediaAsync(string key, string filepath, int timeOut = Config.TIME_OUT)
{
var url = string.Format(Config.ApiWorkHost + "/cgi-bin/webhook/upload_media?key={0}&type=file", key);
var fileDictionary = new Dictionary<string, string>();
fileDictionary["name"] = "media";
fileDictionary["filename"] = filepath;

return await Senparc.CO2NET.HttpUtility.Post.PostFileGetJsonAsync<UploadMediaResult>(CommonDI.CommonSP, url, null, fileDictionary, null, timeOut: timeOut).ConfigureAwait(false);
}
#endregion
}
}
Loading