Module: WechatPublicApi::Kf

Included in:
WechatPublicApi
Defined in:
lib/wechat_public_api/kf_message.rb

Instance Method Summary collapse

Instance Method Details

#kf_image_message(openid, media_id, kf_account = nil) ⇒ Object

发送图片消息

Parameters:

  • media_id (string)

    – 发送的图片/语音/视频/图文消息(点击跳转到图文消息页)的媒体ID



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/wechat_public_api/kf_message.rb', line 64

def kf_image_message(openid, media_id, =nil)

  custom_message = {
    touser: openid,
    msgtype: 'image',
    image: {media_id: media_id}
  }

  if 
    custom_message.merge({customservice:{account: }})
  end

  post_customer_message custom_message
end

#kf_mpnews_message(openid, media_id, kf_account = nil) ⇒ Object

发送图文消息(点击跳转到图文消息页面)



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wechat_public_api/kf_message.rb', line 80

def kf_mpnews_message(openid, media_id, =nil)
  custom_message = {
    touser: openid,
    msgtype: 'mpnews',
    mpnews: {media_id: media_id}
  }
  if 
    custom_message.merge({customservice:{account: }})
  end

  post_customer_message custom_message
end

#kf_news_message(openid, articles, kf_account = nil) ⇒ Object

> example

articles = [

{
    "title":"Happy Day",
    "description":"Is Really A Happy Day",
    "url":"URL",
    "picurl":"PIC_URL"
},
{
    "title":"Happy Day",
    "description":"Is Really A Happy Day",
    "url":"URL",
    "picurl":"PIC_URL"
}

]

发送图文消息(点击跳转到外链)

Parameters:

  • articles (JSON)

    – 图文消息列表



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wechat_public_api/kf_message.rb', line 112

def kf_news_message(openid, articles, =nil)
  custom_message = {
      touser: openid,
      msgtype: 'news',
      news: {
        articles: articles
      }
  }

  if 
    custom_message.merge({customservice:{account: }})
  end
  post_customer_message custom_message
end

#kf_text_message(openid, content, kf_account = nil) ⇒ Object

发送文字消息

Parameters:

  • openid (string)

    – 用户的openid

  • content (string)

    – 需要发送的客服消息内容



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wechat_public_api/kf_message.rb', line 45

def kf_text_message(openid, content, =nil)

  custom_message = {
    touser: openid,
    msgtype: 'text',
    text: {content: content}
  }

  if 
    custom_message.merge({customservice:{account: }})
  end

  post_customer_message custom_message
end

#kf_voice_message(openid, media_id, kf_account = nil) ⇒ Object

发送语音消息



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/wechat_public_api/kf_message.rb', line 128

def kf_voice_message(openid, media_id, =nil)

  custom_message = {
      touser: openid,
      msgtype: 'voice',
      voice: {
        media_id: media_id
      }
  }
  if 
    custom_message.merge({customservice:{account: }})
  end
  post_customer_message custom_message
end

#post_customer_message(message) ⇒ Object

execute post

> example

message =

touser: openid,
msgtype: 'text',
text: {content: content,
customservice:{
  kf_account: 'xxxxxxxx'
}

}

Parameters:

  • message (JSON)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wechat_public_api/kf_message.rb', line 25

def post_customer_message(message)
  # get access_token
  access_token = get_access_token()

  uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=#{access_token}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new("/cgi-bin/message/custom/send?access_token=#{access_token}")
  request.add_field('Content-Type', 'application/json')
  request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
  response = http.request(request)
  JSON.parse(response.body)
end