Beispiel #1
0
 /// <summary>
 /// 默认消息
 /// </summary>
 /// <param name="handler"></param>
 /// <param name="func"></param>
 /// <returns></returns>
 public static RequestMessageTextKeywordHandler Default(this RequestMessageTextKeywordHandler handler, Func <IResponseMessageBase> func)
 {
     if (!handler.MatchSuccessed)
     {
         handler.ResponseMessage = func();
     }
     return(handler);
 }
Beispiel #2
0
 /// <summary>
 /// 匹配选择菜单关键词(对应微信的 SendMenu 接口)
 /// </summary>
 /// <param name="handler"></param>
 /// <param name="keywords">多个关键词(匹配格式:"s:[关键词]")</param>
 /// <param name="func"></param>
 /// <returns></returns>
 public static RequestMessageTextKeywordHandler SelectMenuKeywords(this RequestMessageTextKeywordHandler handler, string[] keywords, Func <IResponseMessageBase> func)
 {
     foreach (var keyword in keywords)
     {
         handler.SelectMenuKeyword(keyword, func);
     }
     return(handler);
 }
Beispiel #3
0
 /// <summary>
 /// 获取最终响应消息
 /// </summary>
 /// <param name="handler"></param>
 /// <returns></returns>
 public static IResponseMessageBase GetResponseMessage(this RequestMessageTextKeywordHandler handler)
 {
     if (!handler.MatchSuccessed &&
         handler.DefaultMessage != null)
     {
         handler.ResponseMessage = handler.DefaultMessage();
     }
     return(handler.ResponseMessage);
 }
Beispiel #4
0
 /// <summary>
 /// 匹配正则表达式
 /// </summary>
 /// <param name="handler"></param>
 /// <param name="pattern">正则表达式</param>
 /// <param name="func"></param>
 /// <returns></returns>
 public static RequestMessageTextKeywordHandler Regex(this RequestMessageTextKeywordHandler handler, string pattern, Func <IResponseMessageBase> func)
 {
     if (!handler.MatchSuccessed &&
         System.Text.RegularExpressions.Regex.IsMatch(handler.Keyword, pattern,
                                                      handler.CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase))
     {
         handler.MatchSuccessed  = true;
         handler.ResponseMessage = func();
     }
     return(handler);
 }
Beispiel #5
0
 /// <summary>
 /// 匹配关键词
 /// </summary>
 /// <param name="handler"></param>
 /// <param name="keyword">关键词</param>
 /// <param name="func"></param>
 /// <returns></returns>
 public static RequestMessageTextKeywordHandler Keyword(this RequestMessageTextKeywordHandler handler, string keyword, Func <IResponseMessageBase> func)
 {
     if (!handler.MatchSuccessed &&
         ((handler.CaseSensitive && handler.Keyword == keyword) ||
          (!handler.CaseSensitive && handler.Keyword.ToUpper() == keyword.ToUpper())))
     {
         handler.MatchSuccessed  = true;
         handler.ResponseMessage = func();
     }
     return(handler);
 }
Beispiel #6
0
        /// <summary>
        /// 匹配选择菜单关键词(对应微信的 SendMenu 接口)
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="keywords">多个关键词(匹配格式:"s:[关键词]")</param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static RequestMessageTextKeywordHandler SelectMenuKeyword(this RequestMessageTextKeywordHandler handler, string keyword, Func <IResponseMessageBase> func)
        {
            if (!(handler.RequestMessage is IRequestMessageSelectMenu))
            {
                throw new Exceptions.MessageHandlerException($"当前请求类型 {handler.RequestMessage.GetType()} 未实现 {nameof(IRequestMessageSelectMenu)} 接口,因此无法使用此方法({nameof(SelectMenuKeyword)})。");
            }

            var finalKeyword = RequestMessageTextKeywordHandler.SELECT_MENU_KEWORD_FORMAT.FormatWith(keyword);

            if (!handler.MatchSuccessed && !handler.SelectMenuId.IsNullOrWhiteSpace() &&
                ((handler.CaseSensitive && handler.SelectMenuId == finalKeyword ||
                  (!handler.CaseSensitive && handler.SelectMenuId.ToUpper() == finalKeyword.ToUpper()))))
            {
                handler.MatchSuccessed  = true;
                handler.ResponseMessage = func();
            }
            return(handler);
        }
Beispiel #7
0
        /// <summary>
        /// 开始匹配
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <param name="caseSensitive">是否大小写敏感,默认为false</param>
        /// <returns></returns>
        public static RequestMessageTextKeywordHandler StartHandler(this IRequestMessageText requestMessage, bool caseSensitive = false)
        {
            var handler = new RequestMessageTextKeywordHandler(requestMessage, caseSensitive);

            return(handler);
        }