messenger_client 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6088f6bbd3711ae7efdeaf6410498abcdc0d21d0
4
- data.tar.gz: 550e200908ed2db624eab4c54ca306ec000ab4eb
3
+ metadata.gz: 9abd4f17e15ca59491666621b4a19e9b21906366
4
+ data.tar.gz: 4fe280d04a1eef1bd8d66abcfd428071e123cd2d
5
5
  SHA512:
6
- metadata.gz: 1d417cfb9eba35ead6eb9d7bbdda17763d5d17f4f10a8ca8207b52b06e99f2d77b63bc85766fea41452459d052b5e44a844ae3457d5fd7dfcd67d2c0e2300df1
7
- data.tar.gz: 6236d6611b1807b469e081755a26069ad15b6496705f54037daeac78653b09cff19e2a734e25f9c5153b416209bddfeae2215196eb045478adc9e886b3f5b31d
6
+ metadata.gz: 14d7f640596a2ecac8ee30ebbd002e9817ae1b6539a1b4ebe29457cec0f5dd6952b6d891beb1db30688a422a348377f1ad95baaf8eb1f36487baf5f318ccd8c9
7
+ data.tar.gz: 87a76d3effaddff9eaf1ba2d54d2d06c2681905ab8187c9ed014417c6ff7ae4e1efcae27442337786f76083c43d9294b69fb34c0a50f65f58e937f3b2486882e
@@ -31,6 +31,11 @@ module MessengerClient
31
31
  send(recipient_id, template.to_json)
32
32
  end
33
33
 
34
+ def generic_template(recipient_id:, title:, subtitle: nil, image_url: nil, default_url: nil, buttons: [])
35
+ template = GenericTemplate.new(title, subtitle, image_url, default_url, buttons)
36
+ send(recipient_id, template.to_json)
37
+ end
38
+
34
39
  def image(recipient_id:, url:)
35
40
  media = URLImage.new(url)
36
41
  send(recipient_id, media.to_json)
@@ -0,0 +1,43 @@
1
+ module MessengerClient
2
+ class GenericTemplate
3
+ def initialize(title, subtitle = nil, image_url = nil, default_url = nil, buttons = [])
4
+ @title = title
5
+ @subtitle = subtitle
6
+ @image_url = image_url
7
+ @default_url = default_url
8
+ @buttons = buttons
9
+ end
10
+
11
+ def to_json
12
+ payload = {
13
+ template_type: "generic",
14
+ title: @title,
15
+ }
16
+
17
+ payload.merge!(subtitle: @subtitle) unless @subtitle.nil?
18
+ payload.merge!(image_url: @image_url) unless @image_url.nil?
19
+ payload.merge!(default_action: parse_default_action(@default_url)) unless @default_url.nil?
20
+ payload.merge!(buttons: parse_buttons(@buttons)) unless @buttons.empty?
21
+
22
+ {
23
+ attachment: {
24
+ type: "template",
25
+ payload: payload
26
+ }
27
+ }
28
+ end
29
+
30
+ private
31
+
32
+ def parse_default_action(url)
33
+ {
34
+ type: "web_url",
35
+ url: url,
36
+ }
37
+ end
38
+
39
+ def parse_buttons(buttons)
40
+ buttons.map(&:to_json)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Audio = Base.new(:id, :timestamp, :sender, :url)
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module MessengerClient
2
+ module Message
3
+ class Base < Struct
4
+ def postback; respond_to?(:_postback) ? _postback : nil; end
5
+ def text; respond_to?(:_text) ? _text : nil; end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ File = Base.new(:id, :timestamp, :sender, :url)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Image = Base.new(:id, :timestamp, :sender, :url, :sticker_id)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Location = Base.new(:id, :timestamp, :sender, :url, :lat, :lng)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Postback = Base.new(:timestamp, :sender, :_postback)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ QuickReply = Base.new(:id, :timestamp, :sender, :_postback, :_text)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Sender = Base.new(:id)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Text = Base.new(:id, :timestamp, :sender, :_text)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module MessengerClient
2
+ module Message
3
+ Video = Base.new(:id, :timestamp, :sender, :url)
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require "messenger_client/message/base"
2
+ require "messenger_client/message/audio"
3
+ require "messenger_client/message/file"
4
+ require "messenger_client/message/image"
5
+ require "messenger_client/message/location"
6
+ require "messenger_client/message/postback"
7
+ require "messenger_client/message/quick_reply"
8
+ require "messenger_client/message/sender"
9
+ require "messenger_client/message/text"
10
+ require "messenger_client/message/video"
@@ -0,0 +1,67 @@
1
+ module MessengerClient
2
+ class MessageParser
3
+ attr_reader :payload
4
+ # Takes in JSON parsed Hash from facebook
5
+ def initialize(payload)
6
+ @payload = payload
7
+ end
8
+
9
+ # Parses Hash messages in payload into list of appropriate Message type
10
+ def parse
11
+ e = payload["entry"].first
12
+ e["messaging"].map { |message|
13
+ parse_message(message)
14
+ }.compact
15
+ end
16
+
17
+ def parse_message(message)
18
+ sender = MessengerClient::Message::Sender.new(message["sender"]["id"])
19
+ timestamp = message["timestamp"]
20
+ m = message["message"]
21
+ if m.nil?
22
+ return parse_postback(message, timestamp, sender)
23
+ end
24
+ if attchs = m["attachments"]
25
+ attch = attchs.first
26
+ pl = attch["payload"]
27
+ url = pl["url"]
28
+ case attch["type"]
29
+ when "image"
30
+ MessengerClient::Message::Image.new(m["mid"], timestamp, sender, url, pl["sticker_id"])
31
+ when "video"
32
+ MessengerClient::Message::Video.new(m["mid"], timestamp, sender, url)
33
+ when "audio"
34
+ MessengerClient::Message::Audio.new(m["mid"], timestamp, sender, url)
35
+ when "file"
36
+ MessengerClient::Message::File.new(m["mid"], timestamp, sender, url)
37
+ when "location"
38
+ url = attch["url"]
39
+ lat, lng = pl["coordinates"]["lat"], pl["coordinates"]["long"]
40
+ MessengerClient::Message::Location.new(m["mid"], timestamp, sender, url, lat, lng)
41
+ else
42
+ end
43
+ else
44
+ parse_non_media(m, timestamp, sender)
45
+ end
46
+ end
47
+
48
+ def parse_non_media(message, timestamp, sender)
49
+ if qr = message["quick_reply"]
50
+ payload = qr["payload"].to_sym
51
+ return MessengerClient::Message::QuickReply.new(message["mid"], timestamp, sender, payload, message["text"])
52
+ end
53
+ if txt = message["text"]
54
+ return MessengerClient::Message::Text.new(message["mid"], timestamp, sender, txt)
55
+ end
56
+ return nil
57
+ end
58
+
59
+ def parse_postback(message, timestamp, sender)
60
+ if pb = message["postback"]
61
+ payload = pb["payload"].to_sym
62
+ return MessengerClient::Message::Postback.new(timestamp, sender, payload)
63
+ end
64
+ return nil
65
+ end
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
1
  module MessengerClient
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -3,6 +3,12 @@ require "typhoeus"
3
3
 
4
4
  require "messenger_client/version"
5
5
 
6
+ # Message Types
7
+ require "messenger_client/message"
8
+
9
+ # Message Parser
10
+ require "messenger_client/message_parser"
11
+
6
12
  # Config
7
13
  require "messenger_client/config"
8
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: messenger_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Cruz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-24 00:00:00.000000000 Z
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,18 @@ files:
96
96
  - lib/messenger_client/logout_button.rb
97
97
  - lib/messenger_client/menu.rb
98
98
  - lib/messenger_client/menu_button.rb
99
+ - lib/messenger_client/message.rb
100
+ - lib/messenger_client/message/audio.rb
101
+ - lib/messenger_client/message/base.rb
102
+ - lib/messenger_client/message/file.rb
103
+ - lib/messenger_client/message/image.rb
104
+ - lib/messenger_client/message/location.rb
105
+ - lib/messenger_client/message/postback.rb
106
+ - lib/messenger_client/message/quick_reply.rb
107
+ - lib/messenger_client/message/sender.rb
108
+ - lib/messenger_client/message/text.rb
109
+ - lib/messenger_client/message/video.rb
110
+ - lib/messenger_client/message_parser.rb
99
111
  - lib/messenger_client/payload_button.rb
100
112
  - lib/messenger_client/quick_replies.rb
101
113
  - lib/messenger_client/quick_reply.rb
@@ -127,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
139
  version: '0'
128
140
  requirements: []
129
141
  rubyforge_project:
130
- rubygems_version: 2.6.8
142
+ rubygems_version: 2.5.2
131
143
  signing_key:
132
144
  specification_version: 4
133
145
  summary: A client for the Facebook Messenger platform