Class: Wechat::Message

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

Overview

Methods for the Contact API

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from DataObject

get, parameterize_params, post, #request

Class Attribute Details

.message_hashObject (readonly)

Returns the value of attribute message_hash.



9
10
11
# File 'lib/wechat/data_objects/message.rb', line 9

def message_hash
  @message_hash
end

Class Method Details

.from_hash(post_xml) ⇒ Object



12
13
14
# File 'lib/wechat/data_objects/message.rb', line 12

def from_hash(post_xml)
    @message_hash = MultiXml.parse(post_xml)
end

.process_message(to, message) ⇒ Object



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/wechat/data_objects/message.rb', line 35

def process_message(to, message)
  @new_message = {
    :touser=>to
  }

  type = message.keys[0]
  body = message.values[0]

  case type
  
  when :text
    @new_message.merge!(:msgtype=>"text", :text=>{:content=>body})
  when :image
    @new_message.merge!(:msgtype=>"image", :image=>{:media_id=>body})
  when :audio
    @new_message.merge!(:msgtype=>"voice", :voice=>{:media_id=>body})
  when :video
    @new_message.merge!(:msgtype=>"video", :video=>{:media_id=>body})
  when :news
    @new_message.merge!(:msgtype=>"news", :news=>{:articles=>body})
  end

  
end

.send(to = '', message = {}) ⇒ Object



18
19
20
21
22
# File 'lib/wechat/data_objects/message.rb', line 18

def send(to='', message = {})
  processed_message = process_message(to,message)
  post( "message/custom/send", processed_message)

end

.verify_signature(token) ⇒ Object



25
26
27
28
# File 'lib/wechat/data_objects/message.rb', line 25

def verify_signature(token)
  array = [token, params[:timestamp], params[:nonce]].compact.sort
  render :text => "Forbidden", :status => 403 if params[:signature] != Digest::SHA1.hexdigest(array.join)
end