fabychy 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +159 -0
- data/lib/fabychy/bot.rb +1 -1
- data/lib/fabychy/sanitizer.rb +11 -11
- data/lib/fabychy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 181d4fef07c43d43edb2c4e8f231c767d2f6b389
|
4
|
+
data.tar.gz: a50bbbca768f2aa4adf523237d143660b4406af7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da317f6104a82b6594951bb53028eca5b44da70442301b65846d90e632a4713fce795ca7a58288ec7fc3ae09030a145ec161f995bc2b700052f8f31b5a584c15
|
7
|
+
data.tar.gz: 7d3e72f81a6a02d7e0e966d45728ad87ee3d790fc1f625f6f7d19123f0b35e88fa6b362a6bdba38bc4987d8c27cf125d49cf481b16dbb16f694669b24459a568
|
data/README.md
CHANGED
@@ -0,0 +1,159 @@
|
|
1
|
+
<!--
|
2
|
+
Title: fabychy
|
3
|
+
Description: A ruby client for Facebook Messenger's bot API
|
4
|
+
Author: Nima Kaviani - nkaviani
|
5
|
+
-->
|
6
|
+
<meta name='keywords' content='fabychy, facebook, bot, ruby, api, library'>
|
7
|
+
<table border="0">
|
8
|
+
<tr>
|
9
|
+
<td width="200px">
|
10
|
+
<img src="https://s3-us-west-2.amazonaws.com/robochyassets/fabychy/fabychu-150x150.png" alt="Rubychy" height="150px"/>
|
11
|
+
</td>
|
12
|
+
<td>
|
13
|
+
<h1>fabychy</h1>
|
14
|
+
A ruby client for <a href="https://messengerplatform.fb.com/">Facebook Messenger's bot API</a> provided by <a href="http://robochy.com">Robochy</a>.
|
15
|
+
</td>
|
16
|
+
</tr>
|
17
|
+
</table>
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add the gem to your application's Gemfile
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'fabychy'
|
25
|
+
```
|
26
|
+
|
27
|
+
and run the following
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
$ bundle install
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
Create a bot using [Facebook Messenger](https://developers.facebook.com/docs/messenger-platform/product-overview) developer portal. Once the registration is complete, copy the `Page Access Token` to be used by this library.
|
35
|
+
|
36
|
+
With your `Page Access Token` you can use `fabychy` like the following:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'fabychy'
|
40
|
+
|
41
|
+
bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
|
42
|
+
```
|
43
|
+
|
44
|
+
## Get User Information
|
45
|
+
You need to have the `USER_ID` for the target user and then call the `get_user` function:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
require 'fabychy'
|
49
|
+
|
50
|
+
bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
|
51
|
+
bot.get_user('[USER_ID]')
|
52
|
+
```
|
53
|
+
|
54
|
+
## Sending Messages
|
55
|
+
With a created bot you can create messages of different types, attach custom keyboards, and pass them to the `send_message` function as follows:
|
56
|
+
```ruby
|
57
|
+
require 'fabychy'
|
58
|
+
|
59
|
+
bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
|
60
|
+
message = Fabychy::Message.new(
|
61
|
+
recipient: {id: [USER_ID] },
|
62
|
+
message: {
|
63
|
+
text: "hi there"
|
64
|
+
},
|
65
|
+
notification_type: Fabychy::Notification::REGULAR
|
66
|
+
)
|
67
|
+
```
|
68
|
+
|
69
|
+
The following notification types can be used:
|
70
|
+
```ruby
|
71
|
+
Fabychy::Notification::REGULAR
|
72
|
+
Fabychy::Notification::SILENT_PUSH
|
73
|
+
Fabychy::Notification::NO_PUSH
|
74
|
+
```
|
75
|
+
|
76
|
+
## DataTypes and Validations
|
77
|
+
Fabychy provides some light-weight validation of the generated message. However, when there is too much scaffolding it also allows you to pass the hash object instead of an actual element to create your messages. Below is an example:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
require 'fabychy/message'
|
81
|
+
|
82
|
+
bot = Fabychy::Bot.new('[PAGE ACCESS TOKEN]')
|
83
|
+
message = Fabychy::Message.new(
|
84
|
+
recipient: {id: "1056613714384965"},
|
85
|
+
message: Fabychy::DataTypes::MessageBody.new(
|
86
|
+
attachment: Fabychy::DataTypes::TemplateAttachment.new(
|
87
|
+
payload: Fabychy::DataTypes::GenericTemplatePayload.new(
|
88
|
+
elements: [
|
89
|
+
Fabychy::DataTypes::GenericElement.new(
|
90
|
+
title: 123,
|
91
|
+
image_url: "http://wip.org/media/nima-456.jpg",
|
92
|
+
subtitle: "This thing is great!",
|
93
|
+
buttons: [
|
94
|
+
Fabychy::DataTypes::Button.new(
|
95
|
+
type: "web_url",
|
96
|
+
title: "robochy",
|
97
|
+
url: "http://robochy.com"
|
98
|
+
)
|
99
|
+
]
|
100
|
+
),
|
101
|
+
Fabychy::DataTypes::GenericElement.new(
|
102
|
+
title: "hello",
|
103
|
+
image_url: "http://wip.org/media/nima-456.jpg",
|
104
|
+
subtitle: "This thing a second thing great!",
|
105
|
+
)
|
106
|
+
]
|
107
|
+
)
|
108
|
+
)
|
109
|
+
)
|
110
|
+
)
|
111
|
+
bot.send_message(message)
|
112
|
+
```
|
113
|
+
|
114
|
+
### Allowed Object Types
|
115
|
+
|
116
|
+
#### Attachments
|
117
|
+
```ruby
|
118
|
+
Fabychy::DataTypes::TemplateAttachment
|
119
|
+
# OR
|
120
|
+
Fabychy::DataTypes::ImageAttachment
|
121
|
+
```
|
122
|
+
#### Payloads
|
123
|
+
```ruby
|
124
|
+
# use the following for Fabychy::DataTypes::ImageAttachment
|
125
|
+
Fabychy::DataTypes::ImagePayload
|
126
|
+
|
127
|
+
# use the following for Fabychy::DataTypes::TemplateAttachment
|
128
|
+
Fabychy::DataTypes::GenericTemplatePayload
|
129
|
+
Fabychy::DataTypes::ButtonTemplatePayload
|
130
|
+
Fabychy::DataTypes::ReceiptTemplatePayload
|
131
|
+
```
|
132
|
+
|
133
|
+
#### Elements
|
134
|
+
```ruby
|
135
|
+
# for Fabychy::DataTypes::GenericTemplatePayload
|
136
|
+
Fabychy::DataTypes::GenericElement
|
137
|
+
|
138
|
+
# for Fabychy::DataTypes::ReceiptTemplatePayload
|
139
|
+
Fabychy::DataTypes::ReceiptElement
|
140
|
+
```
|
141
|
+
|
142
|
+
## Parsing the Response
|
143
|
+
|
144
|
+
In your callback servlet, pass the received `request` (not the request body) to the `Fabychy::ApiResponse.parse` function and you will get the hash response back:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
class Simple < WEBrick::HTTPServlet::AbstractServlet
|
148
|
+
def do_POST(request, response)
|
149
|
+
response = Fabychy::ApiResponse.parse(request) # response is of type Hash
|
150
|
+
end
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
## Contributing
|
155
|
+
- Fork it: https://github.com/nkaviani/fabychy/fork
|
156
|
+
- Create your feature branch (`git checkout -b my-new-feature`)
|
157
|
+
- Commit your changes (`git commit -am 'Add some feature'`)
|
158
|
+
- Push to the branch (`git push origin my-new-feature`)
|
159
|
+
- Create a new Pull Request
|
data/lib/fabychy/bot.rb
CHANGED
@@ -13,7 +13,7 @@ module Fabychy
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def send_message(message)
|
16
|
-
api_post("me/messages?access_token=#{@access_token}", Fabychy::Sanitizer.sanitize(message))
|
16
|
+
api_post("me/messages?access_token=#{@access_token}", Fabychy::Sanitizer.sanitize(:msg, message))
|
17
17
|
end
|
18
18
|
|
19
19
|
def get_user(user_id)
|
data/lib/fabychy/sanitizer.rb
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
module Fabychy
|
2
2
|
class Sanitizer
|
3
3
|
|
4
|
-
def self.sanitize(object)
|
4
|
+
def self.sanitize(k, object)
|
5
5
|
return object if object.class.name !~ /Fabychy::/
|
6
|
-
internal = sanitize_internal(object)
|
6
|
+
internal = sanitize_internal(k, object)
|
7
7
|
h = Hash.new
|
8
8
|
object.attributes.each do |k, att|
|
9
9
|
next unless internal.nil? || internal.key?(k)
|
10
10
|
if !att.is_a? Array
|
11
|
-
h[k]=sanitize(att)
|
11
|
+
h[k]=sanitize(k, att)
|
12
12
|
else
|
13
13
|
h[k]=Array.new
|
14
14
|
att.each do |attr|
|
15
|
-
h[k] << sanitize(attr)
|
15
|
+
h[k] << sanitize(k, attr)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
19
19
|
return h
|
20
20
|
end
|
21
21
|
|
22
|
-
def self.sanitize_internal(object)
|
22
|
+
def self.sanitize_internal(k, object)
|
23
23
|
return if !object.methods.include?(:validations)
|
24
24
|
# Delete params not accepted by the API
|
25
25
|
validated_param = object.to_hash.delete_if do |k, _v|
|
@@ -36,29 +36,29 @@ module Fabychy
|
|
36
36
|
object.validations.each do |key, _value|
|
37
37
|
next if ignored_exclusive.include?(key)
|
38
38
|
if _value[:required] && (!validated_param.key?(key) || validated_param[key].nil?)
|
39
|
-
fail Fabychy::Errors::MissingParamsError.new(key, object.class.name)
|
39
|
+
fail Fabychy::Errors::MissingParamsError.new("#{k}:#{key}", object.class.name)
|
40
40
|
end
|
41
41
|
|
42
42
|
# Check param types
|
43
43
|
unless _value[:class].include?(validated_param[key].class) || (_value[:drop_empty] && validated_param[key].nil?)
|
44
|
-
fail Fabychy::Errors::InvalidParamTypeError.new(key, validated_param[key].class, _value[:class])
|
44
|
+
fail Fabychy::Errors::InvalidParamTypeError.new("#{k}:#{key}", validated_param[key].class, _value[:class])
|
45
45
|
end
|
46
46
|
|
47
|
-
if _value[:class] && _value[:class].include?(Array)
|
47
|
+
if _value[:class] && _value[:class].include?(Array) && validated_param[key]
|
48
48
|
if validated_param[key].empty?
|
49
|
-
fail Fabychy::Errors::InvalidParamValueError.new(key, validated_param[key], ["should not be empty"])
|
49
|
+
fail Fabychy::Errors::InvalidParamValueError.new("#{k}:#{key}", validated_param[key], ["should not be empty"])
|
50
50
|
end
|
51
51
|
if _value[:inner_class]
|
52
52
|
validated_param[key].each do |v|
|
53
53
|
unless _value[:inner_class].include?(v.class)
|
54
|
-
fail Fabychy::Errors::InvalidParamTypeError.new(key, validated_param[key].class, _value[:inner_class])
|
54
|
+
fail Fabychy::Errors::InvalidParamTypeError.new("#{k}:#{key}", validated_param[key].class, _value[:inner_class])
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
60
|
unless _value[:in].nil? || _value[:in].include?(validated_param[key])
|
61
|
-
fail Fabychy::Errors::InvalidParamValueError.new(key, validated_param[key], _value[:in])
|
61
|
+
fail Fabychy::Errors::InvalidParamValueError.new("#{k}:#{key}", validated_param[key], _value[:in])
|
62
62
|
end
|
63
63
|
validated_param[key] = validated_param[key].to_s if _value[:class] == Fixnum
|
64
64
|
end
|
data/lib/fabychy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fabychy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nima Kaviani
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpclient
|