public override IWorkResponseMessageBase DefaultResponseMessage(IWorkRequestMessageBase requestMessage) { var responseMessage = this.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "这是一条没有找到合适回复信息的默认消息。"; return(responseMessage); }
/// <summary> /// 默认消息 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IWorkResponseMessageBase DefaultResponseMessage(IWorkRequestMessageBase requestMessage) { var responseMessage = this.CreateResponseMessage <ResponseMessageText>(); responseMessage.Content = "您发送的消息类型暂未被识别。"; return(responseMessage); }
private static WorkResponseMessageBase CreateFromRequestMessage(IWorkRequestMessageBase requestMessage, ResponseMsgType msgType) { //注意:SDK 内部此方法仍然是重要的最终执行的方法,只是提供了简化的重写方法,不建议外部直接调用。 WorkResponseMessageBase responseMessage = null; try { switch (msgType) { case ResponseMsgType.Text: responseMessage = new ResponseMessageText(); break; case ResponseMsgType.News: responseMessage = new ResponseMessageNews(); break; case ResponseMsgType.Image: responseMessage = new ResponseMessageImage(); break; case ResponseMsgType.Voice: responseMessage = new ResponseMessageVoice(); break; case ResponseMsgType.Video: responseMessage = new ResponseMessageVideo(); break; case ResponseMsgType.MpNews: responseMessage = new ResponseMessageMpNews(); break; case ResponseMsgType.NoResponse: responseMessage = new WorkResponseMessageNoResponse(); break; default: throw new UnknownRequestMsgTypeException(string.Format("ResponseMsgType没有为 {0} 提供对应处理程序。", msgType), new ArgumentOutOfRangeException()); } responseMessage.ToUserName = requestMessage.FromUserName; responseMessage.FromUserName = requestMessage.ToUserName; responseMessage.CreateTime = SystemTime.Now; //使用当前最新时间 } catch (Exception ex) { throw new WeixinException("CreateFromRequestMessage过程发生异常", ex); } return(responseMessage); }
/// <summary> /// 获取响应类型实例,并初始化 /// </summary> /// <typeparam name="T">需要返回的类型</typeparam> /// <param name="requestMessage">请求数据</param> /// <returns></returns> public static T CreateFromRequestMessage <T>(IWorkRequestMessageBase requestMessage) where T : WorkResponseMessageBase { try { var tType = typeof(T); var responseName = tType.Name.Replace("ResponseMessage", ""); //请求名称 ResponseMsgType msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), responseName); return(CreateFromRequestMessage(requestMessage, msgType) as T); } catch (Exception ex) { throw new WeixinException("ResponseMessageBase.CreateFromRequestMessage<T>过程发生异常!", ex); } }
/// <summary> /// 默认返回消息(当任何OnXX消息没有被重写,都将自动返回此默认消息) /// </summary> public abstract IWorkResponseMessageBase DefaultResponseMessage(IWorkRequestMessageBase requestMessage);
/// <summary> /// ResponseMessageBase.CreateFromRequestMessage<T>(requestMessage)的扩展方法 /// </summary> /// <typeparam name="T">需要生成的ResponseMessage类型</typeparam> /// <param name="requestMessage">IRequestMessageBase接口下的接收信息类型</param> /// <returns></returns> public static T CreateResponseMessage <T>(this IWorkRequestMessageBase requestMessage) where T : WorkResponseMessageBase { return(WorkResponseMessageBase.CreateFromRequestMessage <T>(requestMessage)); }
public virtual async Task <IWorkResponseMessageBase> DefaultResponseMessageAsync( IWorkRequestMessageBase requestMessage) { return(await Task.Run(() => DefaultResponseMessage(requestMessage)).ConfigureAwait(false)); }