module EricWeixin::MultCustomer

多客服模块

Public Class Methods

get_customer_service_messages(options) click to toggle source

根据指定条件获取指定用户客服的聊天记录.

参数说明

  • weixin_public_account_id/app_id/weixin_number #公众账号id/公众账号app_id/公众账号number 三个必须有一个

  • openid #普通用户的标识,对当前公众号唯一

  • starttime #聊天的开始时间,datetime类型

  • endtime #聊天的结束时间,datetime类型

  • pageindex #查询第几页,从1开始

  • pagesize #每页大小,每页最多拉取50条

调用说明

首先强调的是starttime与endtime时间跨度不能超过一天,就是说两个时间点都必须是同一天的的某个时间点
options a =  {:app_id=>"xxxxxxx", :openid=>"xxxxxx", :endtime=>'2015-6-8 23:00:00'.to_time, :starttime=>'2015-6-8 00:00:00'.to_time, :pageindex=>1}
::EricWeixin::MultCustomer.get_customer_service_messages options
# File lib/eric_weixin/modules/mult_customer.rb, line 15
def self.get_customer_service_messages options
  pa = if options[:weixin_public_account_id].blank?
         if options[:app_id].blank?
           pa = ::EricWeixin::PublicAccount.find_by_weixin_number options[:weixin_number]
           options[:app_id] = pa.weixin_app_id
           pa
         else
           ::EricWeixin::PublicAccount.find_by_weixin_app_id options[:app_id]
         end
       else
         ::EricWeixin::PublicAccount.find(options[:weixin_public_account_id])
       end
  BusinessException.raise '公众账号未查询到' if pa.blank?
  BusinessException.raise '没有指定用户。' if options[:openid].blank?
  endtime = options[:endtime].to_i
  openid = options[:openid]
  pageindex = options[:pageindex]
  pagesize = options[:pagesize].to_i
  pagesize = pagesize <= 0 ? 50 : pagesize > 50 ? 50 :pagesize
  starttime = options[: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) click to toggle source
# File lib/eric_weixin/modules/mult_customer.rb, line 78
def self.send_customer_service_message options
  pa =if options[:app_id].blank?
         pa = ::EricWeixin::PublicAccount.find_by_weixin_number options[:weixin_number]
         options[:app_id] = pa.weixin_app_id
         pa
       else
         ::EricWeixin::PublicAccount.find_by_weixin_app_id options[:app_id]
       end
  BusinessException.raise '公众账号未查询到' if pa.blank?
  token = ::EricWeixin::AccessToken.get_valid_access_token_by_app_id app_id: options[:app_id]
  t_options = {}
  options[:data].each do |k,v|
    t_options[k] = CGI::escape(v.gsub("\"","'"))
  end
  post_data = {
      :touser => options[:openid],
      :msgtype => options[:message_type],
      options[:message_type] => t_options
  }
  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
    message = ::EricWeixin::MessageLog.where(message_id: options[:message_id]).first
    message_id = if message.blank? then
                   nil
                 else
                   message.id
                 end
    ::EricWeixin::MessageLog.create_public_account_send_message_log openid: options[:openid],
                                                                    message_type: options[:message_type],
                                                                    data: options[:data].to_json,
                                                                    process_status: 0,
                                                                    parent_id: message_id,
                                                                    weixin_public_account_id: pa.id
  end
  ''
end