rubotnik 0.1.1 → 0.2.0

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
- SHA1:
3
- metadata.gz: 722a334f07d4832bd7b5c997ec9a466d167a1793
4
- data.tar.gz: e05b518c7dffcbcf98e539f36491322aba63d827
2
+ SHA256:
3
+ metadata.gz: 33af86f4563629621c9a60e41d8ed6e18b57a7bea3049771f1dc4a7d231f09af
4
+ data.tar.gz: f8231e56a6526c0f371360c5824a148db4c302e5c1b58436598ad4fb0d554b83
5
5
  SHA512:
6
- metadata.gz: bd63042e7060157641084648f576ec3d058dc5b0d911575aae11ba0efc960d48635b7e630dfb3ade7cad2d72ad602fa47668d44b9ef4dff9ccf15dc1739001dc
7
- data.tar.gz: 67ddf59877b68e3bffe51c7b5f51e69a33af8d7f2362b4e5cbf97449e3c518e609cf8937900be0015965fa500475a862f0245df14b54e0197c290254ad278973
6
+ metadata.gz: e5b74060181a95947438daca6a069e45d31f29bdda0e352ca51a1aa47b6003a4f4948bb7b5014511cc0fa50bbaa8c80a4a03458a0ef176f7f09c14bb1a01aa44
7
+ data.tar.gz: d86d3550536e110c6aa76c1fea981860c002b7b7e183a55e1dd68cd2c2872fd882518b81e98b78df2388af5038a67cc678dd3e5c5e173ef30aa508cdc3ca269f
data/README.md CHANGED
@@ -88,7 +88,7 @@ A "command" is just a plain Ruby _method_ inside the `Commands` module that has
88
88
  ```ruby
89
89
 
90
90
  # Will match any of the words and their variations
91
- bind "hello", "hi", "bonjour", "привет" to: :my_method_for_greeting
91
+ bind "hello", "hi", "bonjour", "привет", to: :my_method_for_greeting
92
92
 
93
93
  # Will match only if all words are present (ignoring order)
94
94
  bind "what", "time", "is", all: true, to: :tell_time
@@ -370,6 +370,21 @@ image = UI::ImageAttachment.new(img_url)
370
370
  show(image)
371
371
  ```
372
372
 
373
+ ### Open Graph Attachment
374
+
375
+ The Open Graph template allows you to send a structured message with an Open Graph URL, plus an optional button. Currently, only sharing songs is supported. The song will appear in a bubble that allows the message recipient to see album art and preview the song.
376
+
377
+
378
+ ```ruby
379
+ song_url = 'https://open.spotify.com/track/5qqabIl2vWzo9ApSC317sa'
380
+ open_graph_element = UI::FBOpenGraphTemplate.new(song_url, [{title: 'Button', url: 'https://github.com/progapandist/rubotnik', type: 'web_url' }])
381
+ show(open_graph_element)
382
+ ```
383
+
384
+ here's the result:
385
+
386
+ ![opengraph](./docs/open_graph.png)
387
+
373
388
  ## Other events
374
389
 
375
390
  Events other then `message` and `postback` are currently not supported.
Binary file
@@ -1,3 +1,3 @@
1
1
  module Commands
2
2
  # The contents of this module will be overridden by Commands inside host app
3
- end;
3
+ end
@@ -38,6 +38,8 @@ module Rubotnik
38
38
  end
39
39
 
40
40
  def bind(*regex_strings, all: false, to: nil, reply_with: {})
41
+ return if @matched
42
+
41
43
  regexps = regex_strings.map { |rs| /\b#{rs}/i }
42
44
  proceed = regexps.any? { |regex| @message.text =~ regex }
43
45
  proceed = regexps.all? { |regex| @message.text =~ regex } if all
@@ -4,7 +4,7 @@ require_relative 'user'
4
4
  class UserStore
5
5
  include Singleton
6
6
  attr_reader :users
7
-
7
+
8
8
  def initialize
9
9
  @users = []
10
10
  end
@@ -1,3 +1,3 @@
1
1
  module Rubotnik
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/rubotnik.rb CHANGED
@@ -6,8 +6,11 @@ require 'rubotnik/user'
6
6
  require 'rubotnik/cli'
7
7
  require 'rubotnik/generator'
8
8
  require 'rubotnik/autoloader'
9
+ require 'ui/base_ui_element'
10
+ require 'ui/common/has_buttons'
9
11
  require 'ui/fb_button_template'
10
12
  require 'ui/fb_carousel'
13
+ require 'ui/fb_open_graph_template'
11
14
  require 'ui/image_attachment'
12
15
  require 'ui/quick_replies'
13
16
  require 'sinatra'
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/MethodLength
4
+
5
+ module UI
6
+ class BaseUiElement
7
+ # Sends the valid JSON to Messenger API
8
+ def send(user)
9
+ formed = build(user)
10
+ Bot.deliver(formed, access_token: ENV['ACCESS_TOKEN'])
11
+ end
12
+
13
+ # Use this method to return a valid hash and save it for later
14
+ def build(user)
15
+ @template[:recipient][:id] = user.id
16
+ @template
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UI
4
+ module Common
5
+ module HasButtons
6
+ private
7
+
8
+ def parse_buttons(buttons)
9
+ return [] if buttons.nil? || buttons.empty?
10
+ buttons.map do |button|
11
+ button[:type] = button[:type].to_s
12
+ button
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -3,7 +3,7 @@
3
3
  module UI
4
4
  ########################### BUTTON TEMPLATE #############################
5
5
  # https://developers.facebook.com/docs/messenger-platform/send-api-reference/button-template
6
- class FBButtonTemplate
6
+ class FBButtonTemplate < UI::BaseUiElement
7
7
  def initialize(text, buttons)
8
8
  @template = {
9
9
  recipient: {
@@ -21,27 +21,5 @@ module UI
21
21
  }
22
22
  }
23
23
  end
24
-
25
- # Sends the valid JSON to Messenger API
26
- def send(user)
27
- formed = build(user)
28
- Bot.deliver(formed, access_token: ENV['ACCESS_TOKEN'])
29
- end
30
-
31
- # Use this method to return a valid hash and save it for later
32
- def build(user)
33
- @template[:recipient][:id] = user.id
34
- @template
35
- end
36
-
37
- private
38
-
39
- def parse_buttons(buttons)
40
- return [] if buttons.nil? || buttons.empty?
41
- buttons.map do |button|
42
- button[:type] = button[:type].to_s
43
- button
44
- end
45
- end
46
24
  end
47
25
  end
@@ -2,7 +2,9 @@
2
2
  module UI
3
3
  ################## GENERIC TEMPLATE (aka CAROUSEL) #######################
4
4
  # https://developers.facebook.com/docs/messenger-platform/send-api-reference/generic-template
5
- class FBCarousel
5
+ class FBCarousel < UI::BaseUiElement
6
+ include Common::HasButtons
7
+
6
8
  def initialize(elements)
7
9
  @template = {
8
10
  recipient: { id: nil },
@@ -19,18 +21,6 @@ module UI
19
21
  }
20
22
  end
21
23
 
22
- # Sends the valid JSON to Messenger API
23
- def send(user)
24
- template = build(user)
25
- Bot.deliver(template, access_token: ENV['ACCESS_TOKEN'])
26
- end
27
-
28
- # Use this method to return a valid hash and save it for later
29
- def build(user)
30
- @template[:recipient][:id] = user.id
31
- @template
32
- end
33
-
34
24
  # set image aspect ratio to 'square'
35
25
  def square_images
36
26
  @template[:message][:attachment][:payload][:image_aspect_ratio] = 'square'
@@ -53,13 +43,5 @@ module UI
53
43
  elt
54
44
  end
55
45
  end
56
-
57
- def parse_buttons(buttons)
58
- return [] if buttons.nil? || buttons.empty?
59
- buttons.map do |button|
60
- button[:type] = button[:type].to_s
61
- button
62
- end
63
- end
64
46
  end
65
47
  end
@@ -0,0 +1,42 @@
1
+ # rubocop:disable Metrics/MethodLength
2
+
3
+ module UI
4
+ ########################### OPEN GRAPH TEMPLATE #############################
5
+ # https://developers.facebook.com/docs/messenger-platform/send-messages/template/open-graph
6
+ class FBOpenGraphTemplate < UI::BaseUiElement
7
+ include UI::Common::HasButtons
8
+
9
+ def initialize(url, buttons = [])
10
+ @url = url
11
+ @buttons = buttons
12
+
13
+ @template = {
14
+ recipient: {
15
+ id: nil
16
+ },
17
+ message: {
18
+ attachment: {
19
+ type: 'template',
20
+ payload: {
21
+ template_type: 'open_graph',
22
+ elements: elements
23
+ }
24
+ }
25
+ }
26
+ }
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :url, :buttons
32
+
33
+ def elements
34
+ res = { url: url }
35
+
36
+ buttons_payload = parse_buttons(buttons)
37
+ res[:buttons] = buttons_payload if buttons_payload.any?
38
+
39
+ [res]
40
+ end
41
+ end
42
+ end
@@ -1,7 +1,7 @@
1
1
  # rubocop:disable Metrics/MethodLength
2
2
  module UI
3
3
  # https://developers.facebook.com/docs/messenger-platform/send-api-reference/image-attachment
4
- class ImageAttachment
4
+ class ImageAttachment < UI::BaseUiElement
5
5
  def initialize(url)
6
6
  @template = {
7
7
  recipient: {
@@ -17,15 +17,5 @@ module UI
17
17
  }
18
18
  }
19
19
  end
20
-
21
- def send(user)
22
- formed = build(user)
23
- Bot.deliver(formed, access_token: ENV['ACCESS_TOKEN'])
24
- end
25
-
26
- def build(user)
27
- @template[:recipient][:id] = user.id
28
- @template
29
- end
30
20
  end
31
21
  end
data/rubotnik.gemspec CHANGED
@@ -30,4 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_development_dependency "bundler", "~> 1.15"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
32
  spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "factory_bot", "~> 4.0"
34
+ spec.add_development_dependency "pry"
33
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubotnik
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Barnov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-15 00:00:00.000000000 Z
11
+ date: 2018-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: facebook-messenger
@@ -150,6 +150,34 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: factory_bot
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '4.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '4.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
153
181
  description:
154
182
  email:
155
183
  - andy.barnov@gmail.com
@@ -169,6 +197,7 @@ files:
169
197
  - bin/setup
170
198
  - docs/button_template.png
171
199
  - docs/carousel.png
200
+ - docs/open_graph.png
172
201
  - docs/quick_replies.PNG
173
202
  - exe/rubotnik
174
203
  - lib/rubotnik.rb
@@ -182,8 +211,11 @@ files:
182
211
  - lib/rubotnik/user.rb
183
212
  - lib/rubotnik/user_store.rb
184
213
  - lib/rubotnik/version.rb
214
+ - lib/ui/base_ui_element.rb
215
+ - lib/ui/common/has_buttons.rb
185
216
  - lib/ui/fb_button_template.rb
186
217
  - lib/ui/fb_carousel.rb
218
+ - lib/ui/fb_open_graph_template.rb
187
219
  - lib/ui/image_attachment.rb
188
220
  - lib/ui/quick_replies.rb
189
221
  - rubotnik.gemspec
@@ -218,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
250
  version: '0'
219
251
  requirements: []
220
252
  rubyforge_project:
221
- rubygems_version: 2.6.13
253
+ rubygems_version: 2.7.6
222
254
  signing_key:
223
255
  specification_version: 4
224
256
  summary: Ruby "bot-end" micro-framework for Facebook Messenger