Module: EricWeixin::MultCustomer
- Defined in:
- lib/eric_weixin/modules/mult_customer.rb
Overview
多客服模块
Class Method Summary collapse
-
.get_customer_service_messages(options) ⇒ Object
根据指定条件获取指定用户与客服的聊天记录.
-
.send_customer_service_message(options) ⇒ Object
#.
Class Method Details
.get_customer_service_messages(options) ⇒ Object
根据指定条件获取指定用户与客服的聊天记录.
参数说明
-
weixin_public_account_id/app_id/weixin_number #公众账号id/公众账号app_id/公众账号number 三个必须有一个
-
openid #普通用户的标识,对当前公众号唯一
-
starttime #聊天的开始时间,datetime类型
-
endtime #聊天的结束时间,datetime类型
-
pageindex #查询第几页,从1开始
-
pagesize #每页大小,每页最多拉取50条
调用说明
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/eric_weixin/modules/mult_customer.rb', line 15 def self. pa = if [:weixin_public_account_id].blank? if [:app_id].blank? pa = ::EricWeixin::PublicAccount.find_by_weixin_number [:weixin_number] [:app_id] = pa.weixin_app_id pa else ::EricWeixin::PublicAccount.find_by_weixin_app_id [:app_id] end else ::EricWeixin::PublicAccount.find([:weixin_public_account_id]) end BusinessException.raise '公众账号未查询到' if pa.blank? BusinessException.raise '没有指定用户。' if [:openid].blank? endtime = [:endtime].to_i openid = [:openid] pageindex = [:pageindex] pagesize = [:pagesize].to_i pagesize = pagesize <= 0 ? 50 : pagesize > 50 ? 50 :pagesize starttime = [:starttime].to_i post_data = { :endtime => endtime, :openid => openid, :pageindex => pageindex, :pagesize => pagesize, :starttime => starttime } token = ::EricWeixin::AccessToken.get_valid_access_token_by_app_id app_id: pa.weixin_app_id url = "https://api.weixin.qq.com/customservice/msgrecord/getrecord?access_token=#{token}" response = RestClient.post url, post_data.to_json response = JSON.parse response.body if response['errcode'] == 0 record_list = response["recordlist"] unless record_list.blank? record_list.each do |record| record = record.merge(weixin_public_account_id: pa.id) ::EricWeixin::CustomsServiceRecord.create_one record unless ::EricWeixin::CustomsServiceRecord.exist_one record end else return response['errcode'], false end end return response['errcode'], true end |
.send_customer_service_message(options) ⇒ Object
#
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 |
# File 'lib/eric_weixin/modules/mult_customer.rb', line 78 def self. pa =if [:app_id].blank? pa = ::EricWeixin::PublicAccount.find_by_weixin_number [:weixin_number] [:app_id] = pa.weixin_app_id pa else ::EricWeixin::PublicAccount.find_by_weixin_app_id [:app_id] end BusinessException.raise '公众账号未查询到' if pa.blank? token = ::EricWeixin::AccessToken.get_valid_access_token_by_app_id app_id: [:app_id] = {} [:data].each do |k,v| [k] = CGI::escape(v.gsub("\"","'")) end post_data = { :touser => [:openid], :msgtype => [:message_type], [:message_type] => } url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=#{token}" response = RestClient.post url, CGI::unescape(post_data.to_json) response = JSON.parse response.body ::EricWeixin::MessageLog.transaction do = ::EricWeixin::MessageLog.where(message_id: [:message_id]).first = if .blank? then nil else .id end ::EricWeixin::MessageLog. openid: [:openid], message_type: [:message_type], data: [:data].to_json, process_status: 0, parent_id: , weixin_public_account_id: pa.id end '' end |