Class: EricWeixin::ReplyMessageRule
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EricWeixin::ReplyMessageRule
- Defined in:
- app/models/eric_weixin/reply_message_rule.rb
Constant Summary collapse
- KEY_WORD_TYPE_LABEL =
{"text" => '字符', 'regularexpr' => '正则表达式', 'event' => '事件'}
- REPLY_TYPE_LABEL =
{"text" => '静态字符串', 'wx_function' => '动态运行', 'news' => '多图文'}
Class Method Summary collapse
-
.create_reply_message_rule(options) ⇒ Object
创建匹配规则.
-
.get_contact_customer_notice(public_account_id) ⇒ Object
获取提示找客服的语句。.
-
.match_key_words(wx_key_word, public_account_id, receive_message, need_to_mult_service = true) ⇒ Object
用户发送的关键字、 事件关键字匹配处理。 关键字处理分三类: 事件关键字、 正则表达式关键字、 文本严格匹配关键字 关键字匹配好以后,返回类型有三种: 文本、 图文、以及动态运行。.
-
.process_rule(receive_message, public_account) ⇒ Object
处理来自微信端客户所有的处理。.
-
.update_reply_message_rule(rule_id, options) ⇒ Object
更新匹配规则.
Class Method Details
.create_reply_message_rule(options) ⇒ Object
创建匹配规则
20 21 22 23 24 25 26 27 |
# File 'app/models/eric_weixin/reply_message_rule.rb', line 20 def = , [:weixin_public_account_id, :key_word, :reply_message, :key_word_type, :order, :reply_type], is_valid: true ::EricWeixin::ReplyMessageRule.transaction do = ::EricWeixin::ReplyMessageRule.new .save! end end |
.get_contact_customer_notice(public_account_id) ⇒ Object
获取提示找客服的语句。
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'app/models/eric_weixin/reply_message_rule.rb', line 307 def get_contact_customer_notice public_account_id rules = ::EricWeixin::ReplyMessageRule.valid.where weixin_public_account_id: public_account_id, key_word_type: 'text', key_word: 'CONTACT_CUSTOMER_NOTICE' if rules.blank? ::EricWeixin::ReplyMessageRule. weixin_public_account_id: public_account_id, key_word: 'CONTACT_CUSTOMER_NOTICE', reply_message: '请发送:"芝麻开门"来联系客服', key_word_type: 'text', order: 1, reply_type: "text" ::EricWeixin::ReplyMessageRule. weixin_public_account_id: public_account_id, key_word: '芝麻开门', reply_message: 'CONTACT_CUSTOMER_NOTICE', key_word_type: 'text', order: 1, reply_type: "text" return ::EricWeixin::ReplyMessageRule.get_contact_customer_notice public_account_id else rules.first. end end |
.match_key_words(wx_key_word, public_account_id, receive_message, need_to_mult_service = true) ⇒ Object
用户发送的关键字、 事件关键字匹配处理。 关键字处理分三类: 事件关键字、 正则表达式关键字、 文本严格匹配关键字 关键字匹配好以后,返回类型有三种: 文本、 图文、以及动态运行。
need_to_mult_service 如果匹配不到,是否提示转客服。
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/models/eric_weixin/reply_message_rule.rb', line 226 def match_key_words wx_key_word, public_account_id, , need_to_mult_service=true = [:MsgType]||"text" = nil case when 'event' # event类型关键字 event类型关键字不需要匹配正则表达式。 = ::EricWeixin::ReplyMessageRule.order(order: :desc).valid. where(weixin_public_account_id: public_account_id, key_word_type: 'event', key_word: wx_key_word).first else # 对于非event类型的关键字, 按照order 的优先级,逐条比对。 如果order相同,文本类型优先。 匹配成功就停止。 rules = ::EricWeixin::ReplyMessageRule.order(order: :desc).valid. where(weixin_public_account_id: public_account_id, key_word_type: ['regularexpr', 'text']) rules.each do |rule| if rule.key_word_type == 'text' if wx_key_word == rule.key_word = rule break end end if rule.key_word_type == 'regularexpr' regexp = Regexp.new rule.key_word result = regexp.match wx_key_word unless result.blank? = rule break end end end end # if need_to_mult_service # return ::EricWeixin::ReplyMessage::transfer_mult_customer_service ToUserName: receive_message[:FromUserName], # FromUserName: receive_message[:ToUserName] # else # return '' #当匹配不上,也不需要去多客服的时候,就直接返回。 # end reply_msg = if .blank? if need_to_mult_service # 返回给用户,如何唤醒客服。 = ::EricWeixin::ReplyMessageRule.get_contact_customer_notice public_account_id if .blank? '' else ::EricWeixin::ReplyMessage. ToUserName: [:FromUserName], FromUserName: [:ToUserName], Content: end else '' end else if . == 'CONTACT_CUSTOMER_NOTICE' ::EricWeixin::ReplyMessage::transfer_mult_customer_service ToUserName: [:FromUserName], FromUserName: [:ToUserName] else case .reply_type when "text" ::EricWeixin::ReplyMessage. ToUserName: [:FromUserName], FromUserName: [:ToUserName], Content: . when "news" weixin_news = ::EricWeixin::News.find_by_match_key . ::EricWeixin::ReplyMessage:: ToUserName: [:FromUserName], FromUserName: [:ToUserName], news: weixin_news.weixin_articles when "wx_function" ::Weixin::WeixinAutoReplyFunctions.send(., ({:key_word => wx_key_word, :receive_message => })) end end end reply_msg end |
.process_rule(receive_message, public_account) ⇒ Object
处理来自微信端客户所有的处理。
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'app/models/eric_weixin/reply_message_rule.rb', line 42 def process_rule(, public_account) = .clone [:Content] = CGI::escape([:Content]) if not [:Content].blank? business_type = "#{[:MsgType]}~#{[:Event]}" #兼容腾讯的一个坑....有的是MsgId, 有的是MsgID [:MsgId] = [:MsgID] if (![:MsgID].blank? and [:MsgId].blank?) log = ::EricWeixin::MessageLog. openid: [:FromUserName], weixin_public_account_id: public_account.id, message_type: [:MsgType], message_id: [:MsgId] || [:MsgID], data: .to_json, process_status: 0, #在这里假设都处理完毕,由业务引起的更新请在工程的Process中进行修改。 event_name: [:Event], event_key: [:EventKey], #事件值 create_time: [:CreateTime] business_type.to_debug = case business_type #订阅 when /event~subscribe/ user, is_new = ::EricWeixin::WeixinUser.create_weixin_user public_account.id, [:FromUserName], [:EventKey] result = ::Weixin::Process.subscribe , is_new if result == true match_key_words 'subscribe', public_account.id, , false else result end #取消订阅 when /event~unsubscribe/ result = ::Weixin::Process.unsubscribe if result == true ::EricWeixin::WeixinUser.create_weixin_user public_account.id, [:FromUserName] '' else result end #点击消息,点击菜单时响应。 when /event~CLICK/ result = ::Weixin::Process.click_event [:EventKey], if result == true match_key_words [:EventKey], public_account.id, , false else result end #扫描带参数二维码 when /event~SCAN/ result = ::Weixin::Process.scan_event [:EventKey], if result == true match_key_words "scan_#{[:EventKey]}", public_account.id, , false else result end #查看网页事件 when /event~VIEW/ result = ::Weixin::Process.view_event [:EventKey], if result == true '' else result end when /event~kf_close_session/ result = ::Weixin::Process.kv_close_session if result == true '' else result end when /event~kf_create_session/ #待取回客服聊天列表,所以标记为待处理 log.process_status = 1 log.save! result = ::Weixin::Process.kv_create_session if result == true '' else result end #用户自动上报地理位置信息 when /event~LOCATION/ result = ::Weixin::Process.auto_location_event if result == true '' else result end #用户共享地理位置信息 when /location~/ result = ::Weixin::Process.location_event if result == true '' else result end #模板发送完毕通知消息 when /event~TEMPLATESENDJOBFINISH/ ::EricWeixin::TemplateMessageLog. [:FromUserName], [:MsgID], [:Status] ::Weixin::Process.template_send_job_finish '' #文本消息 when /text~/ result = ::Weixin::Process.text_event [:Content], if result == true # if receive_message[:Content] == "我要找客服" # ::EricWeixin::ReplyMessage::transfer_mult_customer_service ToUserName: receive_message[:FromUserName], # FromUserName: receive_message[:ToUserName] # else match_key_words [:Content], public_account.id, # end else result end when /image~/ result = ::Weixin::Process.image_event [:Content], if result == true '' end when /link~/ result = ::Weixin::Process.link_event if result == true '' else result end # 微信小店订单通知。 when /event~merchant_order/ EricWeixin::Xiaodian::Order.create_order result = ::Weixin::Process.get_merchant_order if result == true '' else result end # 群发发送图文推送后,微信服务器返回的结果 when /event~MASSSENDJOBFINISH/ ::EricWeixin::MediaNews.update_media_news_after_sending ::Weixin::Process. '' #暂时识别不了的消息 else "暂时未处理的场景".to_logger .to_logger result = ::Weixin::Process.another_event if result == true match_key_words 'unknow~words', public_account.id, else result end end "message_to_wechat:".to_logger .to_logger unless .to_s.blank? log. = .to_s log.save! end end |
.update_reply_message_rule(rule_id, options) ⇒ Object
更新匹配规则
30 31 32 33 34 35 36 37 38 |
# File 'app/models/eric_weixin/reply_message_rule.rb', line 30 def (rule_id, ) = , [:weixin_public_account_id, :key_word, :reply_message, :key_word_type, :order, :reply_type, :is_valid] ::EricWeixin::ReplyMessageRule.transaction do rule = ::EricWeixin::ReplyMessageRule.find(rule_id) rule.update_attributes() rule.save! rule end end |