From 01dece9d14aa090b9ceb8e149476cf2db13efe0d Mon Sep 17 00:00:00 2001 From: IcedMango Date: Thu, 14 Nov 2024 18:42:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=A7=BB=E9=99=A4TEMPLATE=5FCARD=5FCLICK?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6(=E6=96=87=E6=A1=A3=E4=B8=AD=E6=9C=AA?= =?UTF-8?q?=E6=89=BE=E5=88=B0=E5=AF=B9=E5=BA=94=E6=8F=8F=E8=BF=B0);?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RequestMessageEvent_TemplateCardClick.cs | 33 ------------------- 1 file changed, 33 deletions(-) delete mode 100644 src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardClick.cs diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardClick.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardClick.cs deleted file mode 100644 index 71f8a11a6d..0000000000 --- a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardClick.cs +++ /dev/null @@ -1,33 +0,0 @@ -/*---------------------------------------------------------------- - Copyright (C) 2024 Senparc - - 文件名:RequestMessageEvent_TemplateCardClick.cs - 文件功能描述:模板卡片点击回调事件 - - - 创建标识:LofyLiu - 20240315 -----------------------------------------------------------------*/ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Senparc.Weixin.Work.Entities -{ - public class RequestMessageEvent_TemplateCardClick : RequestMessageEventBase, IRequestMessageEventBase, IRequestMessageEventKey - { - /// - /// 事件类型 - /// - public override Event Event - { - get { return Event.TEMPLATE_CARD_CLICK; } - } - - /// - /// 事件KEY值,与自定义菜单接口中KEY值对应 - /// - public string EventKey { get; set; } - } -} From a682db6cd677100e9a89b3177986d842675b1063 Mon Sep 17 00:00:00 2001 From: IcedMango Date: Thu, 14 Nov 2024 18:43:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=8D=A1=E7=89=87=E5=8F=B3=E4=B8=8A=E8=A7=92?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E4=BA=8B=E4=BB=B6=E6=8E=A8=E9=80=81;=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E6=A8=A1=E6=9D=BF=E5=8D=A1=E7=89=87=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E7=B1=BB=E5=9E=8B;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RequestMessageEvent_TemplateCardEvent.cs | 79 ++++++++++++++++++ ...questMessageEvent_TemplateCardMenuEvent.cs | 47 +++++++++++ .../Senparc.Weixin.Work/Enums.cs | 12 ++- .../DefaultWorkMessageContext.cs | 7 +- .../Async/WorkMessageHandler.Async.cs | 83 +++++++++++++------ .../MessageHandlers/WorkMessageHandler.cs | 17 +++- 6 files changed, 215 insertions(+), 30 deletions(-) create mode 100644 src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardEvent.cs create mode 100644 src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardMenuEvent.cs diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardEvent.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardEvent.cs new file mode 100644 index 0000000000..0da50f7092 --- /dev/null +++ b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardEvent.cs @@ -0,0 +1,79 @@ +/*---------------------------------------------------------------- + Copyright (C) 2024 Senparc + + 文件名:RequestMessageEvent_TemplateCardEvent.cs + 文件功能描述:企业微信-模板卡片事件推送 + + + 创建标识:IcedMango - 20241114 + +----------------------------------------------------------------*/ + +using System.Collections.Generic; +using System.Xml.Serialization; + +namespace Senparc.Weixin.Work.Entities +{ + /// + /// 企业微信-模板卡片事件推送 + /// + public class RequestMessageEvent_TemplateCardEvent : RequestMessageEventBase, IRequestMessageEventBase, IRequestMessageEventKey + { + /// + /// 事件类型(template_card_event,点击模板卡片按钮) + /// + public override Event Event => Event.TEMPLATE_CARD_EVENT; + + /// + /// 与发送模板卡片消息时指定的按钮btn:key值相同 + /// + public string EventKey { get; set; } + + /// + /// 与发送模板卡片消息时指定的task_id相同 + /// + public string TaskId { get; set; } + + /// + /// 通用模板卡片的类型 + /// + public TemplateCard_CardTypeEnum CardType { get; set; } + + /// + /// 用于调用更新卡片接口的ResponseCode,72小时内有效,且只能使用一次 + /// + public string ResponseCode { get; set; } + + /// + /// + /// + [XmlArray("SelectedItems")] + [XmlArrayItem("SelectedItem")] + public List SelectedItems { get; set; } + } + + /// + /// 通用模板卡片的类型 + /// + public enum TemplateCard_CardTypeEnum + { + text_notice, + news_notice, + button_interaction, + vote_interaction, + multiple_interaction + } + + public class TemplateCard_SelectedItem + { + /// + /// 问题的key值 + /// + [XmlElement("QuestionKey")] + public string QuestionKey { get; set; } + + [XmlArray("OptionIds")] + [XmlArrayItem("OptionId")] + public List OptionIds { get; set; } + } +} \ No newline at end of file diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardMenuEvent.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardMenuEvent.cs new file mode 100644 index 0000000000..ddc049ccc3 --- /dev/null +++ b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Entities/Request/Event/RequestMessageEvent_TemplateCardMenuEvent.cs @@ -0,0 +1,47 @@ +/*---------------------------------------------------------------- + Copyright (C) 2024 Senparc + + 文件名:RequestMessageEvent_TemplateCardMenuEvent.cs + 文件功能描述:通用模板卡片右上角菜单事件推送 + + + 创建标识:IcedMango - 20241114 + +----------------------------------------------------------------*/ + +using System.Collections.Generic; +using System.Xml.Serialization; + +namespace Senparc.Weixin.Work.Entities +{ + /// + /// 企业微信-模板卡片事件推送 + /// + public class RequestMessageEvent_TemplateCardMenuEvent : RequestMessageEventBase, IRequestMessageEventBase, IRequestMessageEventKey + { + /// + /// 事件类型(template_card_menu_event,通用模板卡片右上角菜单事件推送) + /// + public override Event Event => Event.TEMPLATE_CARD_MENU_EVENT; + + /// + /// 与发送模板卡片消息时指定的按钮btn:key值相同 + /// + public string EventKey { get; set; } + + /// + /// 与发送模板卡片消息时指定的task_id相同 + /// + public string TaskId { get; set; } + + /// + /// 通用模板卡片的类型 + /// + public TemplateCard_CardTypeEnum CardType { get; set; } + + /// + /// 用于调用更新卡片接口的ResponseCode,72小时内有效,且只能使用一次 + /// + public string ResponseCode { get; set; } + } +} \ No newline at end of file diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Enums.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Enums.cs index ffd8e7e6bf..fdf6cc37a9 100644 --- a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Enums.cs +++ b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/Enums.cs @@ -60,6 +60,9 @@ GroupTaskSentStatus 枚举 修改标识:LofyLiu - 20240315 修改描述:添加枚举“模板卡片点击回调事件” + + 修改标识: IcedMango - 20241114 + 修改描述: 添加: 通用模板卡片右上角菜单事件推送; 修复不正确的通用模板卡片事件推送类型 ----------------------------------------------------------------*/ using static System.Net.WebRequestMethods; @@ -218,9 +221,14 @@ public enum Event MSGAUDIT_NOTIFY, /// - /// 模板卡片点击回调事件 + /// 模板卡片事件推送 + /// + TEMPLATE_CARD_EVENT, + + /// + /// 通用模板卡片右上角菜单事件 /// - TEMPLATE_CARD_CLICK, + TEMPLATE_CARD_MENU_EVENT, } public enum TencentGender diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageContext/DefaultWorkMessageContext.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageContext/DefaultWorkMessageContext.cs index 76cfe70fbc..89939d0efe 100644 --- a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageContext/DefaultWorkMessageContext.cs +++ b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageContext/DefaultWorkMessageContext.cs @@ -238,8 +238,11 @@ public override IWorkRequestMessageBase GetRequestEntityMappingResult(RequestMsg case "MSGAUDIT_NOTIFY": requestMessage = new RequestMessageEvent_MsgAuditNotify(); break; - case "TEMPLATE_CARD_EVENT": //模板卡片回调事件 - requestMessage = new RequestMessageEvent_TemplateCardClick(); + case "TEMPLATE_CARD_EVENT": // 模板卡片事件推送 + requestMessage = new RequestMessageEvent_TemplateCardEvent(); + break; + case "TEMPLATE_CARD_MENU_EVENT": // 通用模板卡片右上角菜单事件 + requestMessage = new RequestMessageEvent_TemplateCardMenuEvent(); break; default://其他意外类型(也可以选择抛出异常) requestMessage = new RequestMessageEventBase(); diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/Async/WorkMessageHandler.Async.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/Async/WorkMessageHandler.Async.cs index 010ce52a62..aac49bd451 100644 --- a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/Async/WorkMessageHandler.Async.cs +++ b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/Async/WorkMessageHandler.Async.cs @@ -1,23 +1,26 @@ /*---------------------------------------------------------------- Copyright (C) 2024 Senparc - + 文件名:WorkMessageHandler.cs 文件功能描述:企业号请求的集中处理方法 - - + + 创建标识:Senparc - 20150313 - + 修改标识:ccccccmd - 20220227 修改描述:v3.14.10 添加异步方法 - + 修改标识:Senparc - 20230914 修改描述:v3.16.4 企业微信三方代开发处理事件: 修复 Async 方法循环调用的 Bug - + 修改标识:IcedMango - 20240229 修改描述:添加: 企业微信会话存档-产生会话回调事件 修改标识:LofyLiu - 20240315 修改描述:添加: 模板卡片点击回调事件 + + 修改标识: IcedMango - 20241114 + 修改描述: 添加: 通用模板卡片右上角菜单事件推送; 修复不正确的通用模板卡片事件推送类型 ----------------------------------------------------------------*/ using Senparc.NeuChar.Context; @@ -38,7 +41,6 @@ public abstract partial class WorkMessageHandler { public override async Task BuildResponseMessageAsync(CancellationToken cancellationToken) { - switch (RequestMessage.MsgType) { case RequestMsgType.Unknown: //第三方回调 @@ -53,15 +55,16 @@ public override async Task BuildResponseMessageAsync(CancellationToken cancellat throw new WeixinException("没有找到合适的消息类型。"); } } - break; + break; //以下是普通信息 case RequestMsgType.Text: { var requestMessage = RequestMessage as RequestMessageText; + ResponseMessage = await OnTextOrEventRequestAsync(requestMessage) ?? await OnTextRequestAsync(requestMessage); } - break; + break; case RequestMsgType.Location: ResponseMessage = await OnLocationRequestAsync(RequestMessage as RequestMessageLocation); break; @@ -83,10 +86,11 @@ public override async Task BuildResponseMessageAsync(CancellationToken cancellat case RequestMsgType.Event: { var requestMessageText = (RequestMessage as IRequestMessageEventBase).ConvertToRequestMessageText(); + ResponseMessage = await OnTextOrEventRequestAsync(requestMessageText) ?? await OnEventRequestAsync(RequestMessage as IRequestMessageEventBase); } - break; + break; default: throw new UnknownRequestMsgTypeException("未知的MsgType请求类型", null); } @@ -182,6 +186,7 @@ public virtual async Task OnEventRequestAsync(IRequest { var strongRequestMessage = RequestMessage as IRequestMessageEventBase; IWorkResponseMessageBase responseMessage = null; + switch (strongRequestMessage.Event) { case Event.CLICK: //菜单点击 @@ -192,7 +197,7 @@ public virtual async Task OnEventRequestAsync(IRequest break; case Event.PIC_PHOTO_OR_ALBUM: //弹出拍照或者相册发图 responseMessage = await OnEvent_PicPhotoOrAlbumRequestAsync( - RequestMessage as RequestMessageEvent_Pic_Photo_Or_Album); + RequestMessage as RequestMessageEvent_Pic_Photo_Or_Album); break; case Event.SCANCODE_PUSH: //扫码推事件 responseMessage = @@ -236,6 +241,7 @@ public virtual async Task OnEventRequestAsync(IRequest break; case Event.change_contact: var ccRequestMessage = RequestMessage as IRequestMessageEvent_Change_Contact_Base; + switch (ccRequestMessage.ChangeType) { case ContactChangeType.create_user: @@ -281,6 +287,7 @@ public virtual async Task OnEventRequestAsync(IRequest //外部联系人事件相关 case Event.CHANGE_EXTERNAL_CONTACT: var cecRequestMessage = RequestMessage as IRequestMessageEvent_Change_ExternalContact_Base; + switch (cecRequestMessage.ChangeType) { case ExternalContactChangeType.add_external_contact: @@ -319,6 +326,7 @@ public virtual async Task OnEventRequestAsync(IRequest break; case Event.CHANGE_EXTERNAL_CHAT: //客户群变更事件 var cechat = RequestMessage as RequestMessageEvent_Change_External_Chat_Base; + switch (cechat.ChangeType) { case ExternalChatChangeType.create: @@ -344,6 +352,7 @@ public virtual async Task OnEventRequestAsync(IRequest break; case Event.CHANGE_EXTERNAL_TAG: //企业客户标签变更事件 var tag = RequestMessage as RequestMessageEvent_Change_External_Tag_Base; + switch (tag.ChangeType) { case ExternalTagChangeType.create: @@ -387,16 +396,21 @@ public virtual async Task OnEventRequestAsync(IRequest OnEvent_Open_Approval_Change_Status_ChangeRequestAsync( RequestMessage as RequestMessageEvent_OpenApprovalChange); break; - + case Event.MSGAUDIT_NOTIFY: //企业微信会话存档-产生会话回调事件 responseMessage = await OnEvent_MsgAuditNotifyRequestAsync( RequestMessage as RequestMessageEvent_MsgAuditNotify); break; - case Event.TEMPLATE_CARD_CLICK://模板卡片点击回调事件 - responseMessage = await - OnEvent_TemplateCardEventClickRequestAsync( - RequestMessage as RequestMessageEvent_TemplateCardClick); + case Event.TEMPLATE_CARD_EVENT: // 企业微信-模板卡片事件推送 + responseMessage = await + OnEvent_TemplateCardEventRequestAsync( + RequestMessage as RequestMessageEvent_TemplateCardEvent); + break; + case Event.TEMPLATE_CARD_MENU_EVENT: // 通用模板卡片右上角菜单事件推送 + responseMessage = await + OnEvent_TemplateCardMenuEventRequestAsync( + RequestMessage as RequestMessageEvent_TemplateCardMenuEvent); break; default: throw new UnknownRequestMsgTypeException("未知的Event下属请求信息", null); @@ -718,6 +732,7 @@ public virtual async Task OnEvent_ChangeExternalChatDi } #region 企业客户标签事件 + /// /// 企业客户标签-创建 /// @@ -762,6 +777,7 @@ public virtual async Task OnEvent_ChangeExternalTagShu { return await Task.Run(() => OnEvent_ChangeExternalTagShuffleRequest(requestMessage)).ConfigureAwait(false); } + #endregion public virtual async Task OnEvent_Living_Status_ChangeRequestAsync( @@ -769,12 +785,14 @@ public virtual async Task OnEvent_Living_Status_Change { return await Task.Run(() => OnEvent_Living_Status_ChangeRequest(requestMessage)).ConfigureAwait(false); } + /// /// 修改设置工作台自定义开关事件推送 /// /// /// - public virtual async Task OnEvent_SwitchWorkbenchModel(RequestMessageEvent_Switch_WorkBench_Mode requestMessage) + public virtual async Task OnEvent_SwitchWorkbenchModel( + RequestMessageEvent_Switch_WorkBench_Mode requestMessage) { return await Task.Run(() => OnEvent_SwitchWorkBenchMode(requestMessage)).ConfigureAwait(false); } @@ -806,26 +824,40 @@ public virtual async Task OnEvent_Open_Approval_Change } #endregion - + /// /// 企业微信会话存档-产生会话回调事件 /// /// /// - public virtual async Task OnEvent_MsgAuditNotifyRequestAsync(RequestMessageEvent_MsgAuditNotify requestMessage) + public virtual async Task OnEvent_MsgAuditNotifyRequestAsync( + RequestMessageEvent_MsgAuditNotify requestMessage) { return await Task.Run(() => OnEvent_MsgAuditNotifyRequest(requestMessage)).ConfigureAwait(false); } /// - /// 模板卡片点击回调事件 + /// 企业微信-模板卡片事件推送 + /// + /// + /// + public virtual async Task OnEvent_TemplateCardEventRequestAsync( + RequestMessageEvent_TemplateCardEvent requestMessage) + { + return await Task.Run(() => OnEvent_TemplateCardEventRequest(requestMessage)).ConfigureAwait(false); + } + + /// + /// 通用模板卡片右上角菜单事件推送 /// /// /// - public virtual async Task OnEvent_TemplateCardEventClickRequestAsync(RequestMessageEvent_TemplateCardClick requestMessage) + public virtual async Task OnEvent_TemplateCardMenuEventRequestAsync( + RequestMessageEvent_TemplateCardMenuEvent requestMessage) { - return await Task.Run(() => OnEvent_TemplateCardEventClickRequest(requestMessage)).ConfigureAwait(false); + return await Task.Run(() => OnEvent_TemplateCardMenuEventRequest(requestMessage)).ConfigureAwait(false); } + #endregion //Event 下属分类 #endregion @@ -849,10 +881,12 @@ private Task OnThirdPartyEventAsync(IThirdPartyInfoBase thirdPartyInfo) case ThirdPartyInfo.REGISTER_CORP: return OnThirdPartyEvent_Register_CorpAsync((RequestMessager_Register_Corp)thirdPartyInfo); case ThirdPartyInfo.RESET_PERMANENT_CODE: - return OnThirdPartEvent_Reset_Permanent_CodeAsync((RequestMessageInfo_Reset_Permanent_Code)thirdPartyInfo); + return OnThirdPartEvent_Reset_Permanent_CodeAsync( + (RequestMessageInfo_Reset_Permanent_Code)thirdPartyInfo); case ThirdPartyInfo.CHANGE_EXTERNAL_CONTACT: { var cecRequestMessage = RequestMessage as IRequestMessageEvent_Change_ExternalContact_Base; + switch (cecRequestMessage.ChangeType) { case ExternalContactChangeType.add_external_contact: @@ -926,7 +960,8 @@ protected virtual async Task OnThirdPartyEvent_Suite_TicketAsync( return await Task.Run(() => OnThirdPartyEvent_Suite_Ticket(thirdPartyInfo)).ConfigureAwait(false); } - protected virtual async Task OnThirdPartEvent_Reset_Permanent_CodeAsync(RequestMessageInfo_Reset_Permanent_Code thirdPartyInfo) + protected virtual async Task OnThirdPartEvent_Reset_Permanent_CodeAsync( + RequestMessageInfo_Reset_Permanent_Code thirdPartyInfo) { return await Task.Run(() => OnThirdPartEvent_ResetPermanentCode(thirdPartyInfo)).ConfigureAwait(false); } diff --git a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/WorkMessageHandler.cs b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/WorkMessageHandler.cs index 7df6acf9aa..61b3e9654f 100644 --- a/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/WorkMessageHandler.cs +++ b/src/Senparc.Weixin.Work/Senparc.Weixin.Work/MessageHandlers/WorkMessageHandler.cs @@ -74,6 +74,9 @@ OnThirdPartyEvent_Contact_Sync 改名为 OnThirdPartyEvent_Change_Contact() 修改标识:LofyLiu - 20240315 修改描述:添加: 模板卡片点击回调事件 + + 修改标识: IcedMango - 20241114 + 修改描述: 添加: 通用模板卡片右上角菜单事件推送; 修复不正确的通用模板卡片事件推送类型 ----------------------------------------------------------------*/ using System; @@ -901,11 +904,21 @@ public virtual IWorkResponseMessageBase OnEvent_MsgAuditNotifyRequest(RequestMes } /// - /// 模板卡片点击回调事件 + /// 模板卡片事件推送 + /// + /// + /// + public virtual IWorkResponseMessageBase OnEvent_TemplateCardEventRequest(RequestMessageEvent_TemplateCardEvent requestMessage) + { + return DefaultResponseMessage(requestMessage); + } + + /// + /// 通用模板卡片右上角菜单事件推送 /// /// /// - public virtual IWorkResponseMessageBase OnEvent_TemplateCardEventClickRequest(RequestMessageEvent_TemplateCardClick requestMessage) + public virtual IWorkResponseMessageBase OnEvent_TemplateCardMenuEventRequest(RequestMessageEvent_TemplateCardMenuEvent requestMessage) { return DefaultResponseMessage(requestMessage); }