Class: Wechat::Broadcast

Inherits:
DataObject show all
Defined in:
lib/wechat/data_objects/broadcast.rb

Overview

Methods for the Advanced Broadcast Interface admin.wechat.com/wiki/index.php?title=Advanced_Broadcast_Interface

Class Method Summary collapse

Methods inherited from DataObject

get, parameterize_params, post, #request

Class Method Details

.generate_content(message) ⇒ Object



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
# File 'lib/wechat/data_objects/broadcast.rb', line 48

def generate_content(message)
  type = message.keys[0]
  body = message.values[0]

  case type

  when :text
  	content = {text: {content: body},msgtype: "text"}
 	when :audio
 		content = {voice: {media_id: upload_media_for_broadcast('voice',body)},msgtype: "voice"}
 	when :image
 		content = {image: {media_id: upload_media_for_broadcast('image',body)},msgtype: "image"}

  when :news
    items = []

    body.each do |article|

      new_article = {
        thumb_media_id: upload_media_for_broadcast('image',article[:picurl]),
        title: article[:title],
        content_source_url: article[:url],
        content: article[:url],
        digest: article[:description],
      }

      items << new_article

    end

    
    news_upload_result = upload_news_articles_for_broadcast(items)

    content = {mpnews: {media_id: news_upload_result["media_id"]},msgtype: "mpnews"}



  end
  
end

.generate_send_list(followers) ⇒ Object

private



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wechat/data_objects/broadcast.rb', line 34

def generate_send_list(followers)
  
  if followers.empty?

    all_followers = Wechat::Follower.list
    send_followers = {touser: all_followers["data"]["openid"]}

  else
    send_followers = {touser: followers}
  end


end

.send(message = {}, followers = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wechat/data_objects/broadcast.rb', line 19

def send(message = {}, followers = [])
	
  to_followers = generate_send_list(followers)
  send_message = generate_content(message)
  b = {}
  b.merge!(to_followers)
  b.merge!(send_message)
  broadcast_message = JSON.generate(b)
  

  post "message/mass/send", broadcast_message

end

.upload_media_for_broadcast(type, item) ⇒ Object



89
90
91
92
# File 'lib/wechat/data_objects/broadcast.rb', line 89

def upload_media_for_broadcast(type,item)
  result = Wechat::Media.upload(type,item)
  return result['media_id']
end

.upload_news_articles_for_broadcast(articles) ⇒ Object



94
95
96
# File 'lib/wechat/data_objects/broadcast.rb', line 94

def upload_news_articles_for_broadcast(articles)
  post "media/uploadnews", JSON.generate({articles: articles})
end