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 +4 -4
- data/lib/messenger_client/client.rb +5 -0
- data/lib/messenger_client/generic_template.rb +43 -0
- data/lib/messenger_client/message/audio.rb +5 -0
- data/lib/messenger_client/message/base.rb +8 -0
- data/lib/messenger_client/message/file.rb +5 -0
- data/lib/messenger_client/message/image.rb +5 -0
- data/lib/messenger_client/message/location.rb +5 -0
- data/lib/messenger_client/message/postback.rb +5 -0
- data/lib/messenger_client/message/quick_reply.rb +5 -0
- data/lib/messenger_client/message/sender.rb +5 -0
- data/lib/messenger_client/message/text.rb +5 -0
- data/lib/messenger_client/message/video.rb +5 -0
- data/lib/messenger_client/message.rb +10 -0
- data/lib/messenger_client/message_parser.rb +67 -0
- data/lib/messenger_client/version.rb +1 -1
- data/lib/messenger_client.rb +6 -0
- metadata +15 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9abd4f17e15ca59491666621b4a19e9b21906366
|
4
|
+
data.tar.gz: 4fe280d04a1eef1bd8d66abcfd428071e123cd2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,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
|
data/lib/messenger_client.rb
CHANGED
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.
|
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-
|
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.
|
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
|