viberroo 0.3.1 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/viberroo/bot.rb +188 -16
- data/lib/viberroo/configuration.rb +31 -6
- data/lib/viberroo/input.rb +79 -0
- data/lib/viberroo/message.rb +154 -0
- data/lib/viberroo/response.rb +28 -1
- data/lib/viberroo/version.rb +1 -1
- data/lib/viberroo.rb +6 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8680e2cafc78f601457863ee3ea61012d102da14d025c232847c0d0f96a391f5
|
4
|
+
data.tar.gz: 5387376c93d2aef3a23707eb936094d07f4c0e93407b116b616ab639b6f6020d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cda037c2fab0c148db8aff216ebbe414239e05c648bc28f3324dd9d35b73e343da61fc9b8cbbcd61add30f3320ba6fdaebccea866f0a63d6b4630e3caeede862
|
7
|
+
data.tar.gz: a4b5427873cc6bf1eb2ca81e8965a54052dc1d01715e19e0928972c0fb0f5be3d700457885027ab08b42d62056bbf14bdc08624bc197554257347e6fb32b309d
|
data/lib/viberroo/bot.rb
CHANGED
@@ -1,12 +1,34 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'uri'
|
3
3
|
|
4
|
-
# Namespace for all Viberroo code.
|
5
4
|
module Viberroo
|
6
|
-
|
7
|
-
#
|
5
|
+
##
|
6
|
+
# This class represents a server bot/client which communicates to Viber API. Each request sends a http POST request to a particular endpoint, each returns either http response, or parsed response body as specified in configuration.
|
7
|
+
#
|
8
|
+
# @see Configuration
|
9
|
+
#
|
8
10
|
class Bot
|
9
|
-
|
11
|
+
##
|
12
|
+
# @example Initializing
|
13
|
+
# class ViberController < ApplicationController
|
14
|
+
# skip_before_action :verify_authenticity_token
|
15
|
+
#
|
16
|
+
# def callback
|
17
|
+
# @response = Viberroo::Response.new(params.permit!)
|
18
|
+
# @bot = Viberroo::Bot.new(response: @response)
|
19
|
+
#
|
20
|
+
# head :ok
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# @param [Response] response **Required**. A callback response.
|
25
|
+
# @param [String] token **Optional**. Normally should be provided by `Viberroo.configure.auth_token` but is available here as a shortcut when predefined configuration is undesirable. Takes precedence over `Viberroo.configure.auth_token`.
|
26
|
+
#
|
27
|
+
# @see Response
|
28
|
+
# @see Configuration
|
29
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#authentication-token
|
30
|
+
#
|
31
|
+
def initialize(response: Response.new({}), token: nil)
|
10
32
|
Viberroo.configure
|
11
33
|
|
12
34
|
@headers = {
|
@@ -16,45 +38,194 @@ module Viberroo
|
|
16
38
|
@response = response
|
17
39
|
end
|
18
40
|
|
19
|
-
|
41
|
+
##
|
42
|
+
# Sets a webhook by making a request to `/set_webhook`. Necessary for receiving callbacks from Viber.
|
43
|
+
# For security reasons only URLs with valid and official SSL certificate from a trusted CA will be allowed. `ngrok` is a good workaround for development convenience.
|
44
|
+
#
|
45
|
+
# @example Setup webhook with rake task
|
46
|
+
# namespace :viber do
|
47
|
+
# task set_webhook: :environment do
|
48
|
+
# Viberroo::Bot.new.set_webhook(
|
49
|
+
# url: 'https://<your_ngrok_public_address>/viber',
|
50
|
+
# event_types: %w[conversation_started subscribed unsubscribed],
|
51
|
+
# send_name: true,
|
52
|
+
# send_photo: true
|
53
|
+
# )
|
54
|
+
# end
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# @param [String] url **Required**. HTTPs callback URL.
|
58
|
+
# @param [Array] event_types **Optional**. Indicates the types of Viber events that the bot will receive. Leaving this parameter out will include all events. **API default**: `%w[delivered seen failed subscribed unsubscribed conversation_started]`.
|
59
|
+
# @param [true, false] send_name **Optional**. Indicates whether or not the bot should receive the user name. **API default**: `false`.
|
60
|
+
# @param [true, false] send_photo **Optional**. Indicates whether or not the bot should receive the user photo. **API default**: `false`.
|
61
|
+
#
|
62
|
+
# @return [Net::HTTPResponse || Hash]
|
63
|
+
#
|
64
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#webhooks
|
65
|
+
#
|
20
66
|
def set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil)
|
21
67
|
request(URL::WEBHOOK, url: url, event_types: event_types,
|
22
|
-
|
68
|
+
send_name: send_name, send_photo: send_photo)
|
23
69
|
end
|
24
70
|
|
25
|
-
|
71
|
+
##
|
72
|
+
# Removes a webhook by making a request to `/set_webhook`.
|
73
|
+
#
|
74
|
+
# @example Remove webhook with rake task
|
75
|
+
# namespace :viber do
|
76
|
+
# task remove_webhook: :environment do
|
77
|
+
# Viberroo::Bot.new.remove_webhook
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#removing-your-webhook
|
82
|
+
#
|
83
|
+
# @return [Net::HTTPResponse || Hash]
|
84
|
+
#
|
26
85
|
def remove_webhook
|
27
86
|
request(URL::WEBHOOK, url: '')
|
28
87
|
end
|
29
88
|
|
30
|
-
|
89
|
+
##
|
90
|
+
# Sends a message to a user by making a request to `/send_message`.
|
91
|
+
#
|
92
|
+
# @example Send a plain message
|
93
|
+
# go_somewhere = Viberroo::Input.url_button({
|
94
|
+
# Columns: 3,
|
95
|
+
# Rows: 2,
|
96
|
+
# Text: 'Mystery link',
|
97
|
+
# ActionBody: 'somewhere.com'
|
98
|
+
# })
|
99
|
+
#
|
100
|
+
# keyboard = Viberroo::Input.keyboard(Buttons: [go_somewhere])
|
101
|
+
# message = Viberroo::Message.plain(text: 'Click if you dare.')
|
102
|
+
#
|
103
|
+
# @bot.send(message: message, keyboard: keyboard)
|
104
|
+
#
|
105
|
+
# @example Send a rich media
|
106
|
+
# search = Viberroo::Input.reply_button({
|
107
|
+
# Columns: 4,
|
108
|
+
# Rows: 3,
|
109
|
+
# ActionBody: '/search',
|
110
|
+
# Text: 'Search something...'
|
111
|
+
# }
|
112
|
+
#
|
113
|
+
# locate = Viberroo::Input.reply_button({
|
114
|
+
# Columns: 4,
|
115
|
+
# Rows: 3,
|
116
|
+
# ActionBody: '/near_me'
|
117
|
+
# }
|
118
|
+
#
|
119
|
+
# browse = Viberroo::Input.url_button({
|
120
|
+
# Columns: 4,
|
121
|
+
# Rows: 2,
|
122
|
+
# ActionBody: 'parrot.live',
|
123
|
+
# Text: 'Browse something wierd'
|
124
|
+
# }
|
125
|
+
#
|
126
|
+
# rich_message = Viberroo::Message.rich(rich_media: { ButtonsGroupColumns: 4,
|
127
|
+
# ButtonsGroupRows: 6,
|
128
|
+
# Buttons: [search, locate, browse] })
|
129
|
+
#
|
130
|
+
# @bot.send(message: rich_message)
|
131
|
+
#
|
132
|
+
# @param [Hash] message **Required**. One of the message types to send.
|
133
|
+
# @param [Hash] keyboard **Optional**. A keyboard that can be attached to a message.
|
134
|
+
#
|
135
|
+
# @return [Net::HTTPResponse || Hash]
|
136
|
+
#
|
137
|
+
# @see Message
|
138
|
+
# @see Input
|
139
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#send-message
|
140
|
+
# @see https://viber.github.io/docs/tools/keyboards/#buttons-parameters
|
141
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#keyboards
|
142
|
+
#
|
143
|
+
def send_message(message, keyboard: {})
|
144
|
+
request(URL::MESSAGE, { receiver: @response.user_id }.merge(message, keyboard))
|
145
|
+
end
|
146
|
+
|
147
|
+
# @deprecated Use {#send_message} instead.
|
31
148
|
def send(message:, keyboard: {})
|
32
|
-
|
33
|
-
|
149
|
+
Viberroo.config.logger.info(<<~WARNING)
|
150
|
+
DEPRECATION WARNING: Bot#send method is going to be removed in the next
|
151
|
+
minor release. Use Bot#send_message instead.
|
152
|
+
WARNING
|
153
|
+
|
154
|
+
send_message(message, keyboard: keyboard)
|
34
155
|
end
|
35
156
|
|
36
|
-
|
157
|
+
##
|
158
|
+
# @note This request has a rate limit of 500 requests in a 10 seconds window.
|
159
|
+
#
|
160
|
+
# Broadcasts a messages to subscribed users by making a request to `/broadcast_message`.
|
161
|
+
#
|
162
|
+
# @example Broadcast simple message
|
163
|
+
# message = Viberroo::Message.plain(text: 'Howdy.')
|
164
|
+
# response = @bot.broadcast(message: message, to: ViberSubscriber.sample(500).pluck(:viber_id))
|
165
|
+
#
|
166
|
+
# @param [Hash] message **Required**. One of the message types to broadcast.
|
167
|
+
# @param [Array] to **Required**. List of user ids to broadcast to. Specified users need to be subscribed.
|
168
|
+
#
|
169
|
+
# @return [Net::HTTPResponse || Hash]
|
170
|
+
#
|
171
|
+
# @see Message
|
172
|
+
# @see Input
|
173
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#broadcast-message
|
174
|
+
#
|
37
175
|
def broadcast(message:, to:)
|
38
176
|
request(URL::BROADCAST_MESSAGE, message.merge(broadcast_list: to))
|
39
177
|
end
|
40
178
|
|
41
|
-
|
179
|
+
##
|
180
|
+
# Retrieves account info by making a request to `/get_account_info`. These settings can be set in you Viber admin panel.
|
181
|
+
#
|
182
|
+
# @return [Net::HTTPResponse || Hash]
|
183
|
+
#
|
184
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#get-account-info
|
185
|
+
#
|
42
186
|
def get_account_info
|
43
187
|
request(URL::GET_ACCOUNT_INFO)
|
44
188
|
end
|
45
189
|
|
46
|
-
|
190
|
+
##
|
191
|
+
# @note This request can be sent twice during a 12 hours period for each user ID.
|
192
|
+
#
|
193
|
+
#
|
194
|
+
# @example
|
195
|
+
# response = @bot.get_user_details(id: ViberSubscriber.sample.viber_id)
|
196
|
+
#
|
197
|
+
# Retrieves details of particular user by making a request to `/get_user_details`.
|
198
|
+
#
|
199
|
+
# @return [Net::HTTPResponse || Hash]
|
200
|
+
#
|
201
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#get-user-details
|
202
|
+
#
|
47
203
|
def get_user_details(id:)
|
48
204
|
request(URL::GET_USER_DETAILS, id: id)
|
49
205
|
end
|
50
206
|
|
51
|
-
|
207
|
+
##
|
208
|
+
# @note The API supports up to 100 user id per request and those users must be subscribed to the account.
|
209
|
+
#
|
210
|
+
# Retrieves a list of user status by making a request to `get_online`.
|
211
|
+
#
|
212
|
+
#
|
213
|
+
# @example
|
214
|
+
# response = @bot.get_online(ids: ViberSubscriber.sample(100).pluck(:viber_id))
|
215
|
+
#
|
216
|
+
# @param [Array] ids **Required**. List of user ids.
|
217
|
+
#
|
218
|
+
# @return [Net::HTTPResponse || Hash]
|
219
|
+
#
|
220
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#get-online
|
221
|
+
#
|
52
222
|
def get_online(ids:)
|
53
223
|
request(URL::GET_ONLINE, ids: ids)
|
54
224
|
end
|
55
225
|
|
56
226
|
private
|
57
227
|
|
228
|
+
# @!visibility private
|
58
229
|
def request(url, params = {})
|
59
230
|
uri = URI(url)
|
60
231
|
|
@@ -65,15 +236,16 @@ module Viberroo
|
|
65
236
|
http.request(request)
|
66
237
|
end
|
67
238
|
|
68
|
-
Viberroo.config.logger&.info("##{caller_name} -- #{response.body}")
|
69
|
-
|
70
239
|
Viberroo.config.parse_response_body ? JSON.parse(response.body) : response
|
71
240
|
end
|
72
241
|
|
242
|
+
# @!visibility private
|
243
|
+
# Extends Ruby version compability from 2.4 to 2.3.
|
73
244
|
def compact(params)
|
74
245
|
params.delete_if { |_, v| v.nil? }
|
75
246
|
end
|
76
247
|
|
248
|
+
# @!visibility private
|
77
249
|
def caller_name
|
78
250
|
caller[1][/`.*'/][1..-2]
|
79
251
|
end
|
@@ -4,34 +4,59 @@ module Viberroo
|
|
4
4
|
attr_accessor :config
|
5
5
|
end
|
6
6
|
|
7
|
-
|
7
|
+
##
|
8
|
+
# Yields the global configuration to a block. Returns existing configuration
|
9
|
+
# if one was defined earlier.
|
8
10
|
# @yield [Configuration] global configuration
|
9
11
|
#
|
10
12
|
# @example
|
11
13
|
# RSpec.configure do |config|
|
12
|
-
# config.auth_token = '
|
14
|
+
# config.auth_token = '445da6az1s345z78-dazcczb2542zv51a-e0vc5fva17480im9'
|
15
|
+
# config.parse_response_body = false
|
13
16
|
# end
|
17
|
+
#
|
14
18
|
# @see Viberroo::Configuration
|
19
|
+
#
|
15
20
|
def self.configure
|
16
21
|
self.config ||= Configuration.new
|
17
22
|
yield(config) if block_given?
|
18
23
|
end
|
19
24
|
|
25
|
+
##
|
20
26
|
# Stores runtime configuration information.
|
27
|
+
#
|
21
28
|
class Configuration
|
22
|
-
|
29
|
+
##
|
30
|
+
# Specifies logger.
|
31
|
+
#
|
32
|
+
# @return [Logger]
|
33
|
+
attr_accessor :logger
|
34
|
+
|
35
|
+
##
|
36
|
+
# Stores Viber API authentication token.
|
37
|
+
# Necessary for the bot to send API requests.
|
38
|
+
#
|
39
|
+
# @return [String]
|
40
|
+
#
|
41
|
+
# @see Bot#set_webhook
|
42
|
+
attr_accessor :auth_token
|
43
|
+
|
44
|
+
##
|
45
|
+
# Specifies whether to parse response body of Bot requests.
|
46
|
+
#
|
47
|
+
# @return [true || false]
|
48
|
+
#
|
49
|
+
# @see Bot
|
50
|
+
attr_accessor :parse_response_body
|
23
51
|
|
24
52
|
def initialize
|
25
|
-
# Stores Viber API authentication token. Necessary for the bot to send API requests.
|
26
53
|
@auth_token = nil
|
27
54
|
|
28
|
-
# Specifies logger.
|
29
55
|
@logger = Logger.new(STDOUT)
|
30
56
|
@logger.formatter = proc do |severity, datetime, _, msg|
|
31
57
|
"[#{datetime}] #{severity} Viberroo::Bot #{msg}\n"
|
32
58
|
end
|
33
59
|
|
34
|
-
# Specifies whether to parse request response body.
|
35
60
|
@parse_response_body = true
|
36
61
|
end
|
37
62
|
end
|
data/lib/viberroo/input.rb
CHANGED
@@ -1,29 +1,108 @@
|
|
1
1
|
module Viberroo
|
2
|
+
##
|
3
|
+
# This class' methods serve as declarative wrappers with predefined
|
4
|
+
# types for UI elements such as buttons and keyboards. Buttons can be combined
|
5
|
+
# with a keyboard or used in rich messages. Only basic parameters are
|
6
|
+
# specified in this documentation, to see all possibilities please consult
|
7
|
+
# official Viber API documentation.
|
8
|
+
#
|
9
|
+
# @see https://viber.github.io/docs/tools/keyboards/#general-keyboard-parameters
|
10
|
+
# @see https://viber.github.io/docs/tools/keyboards/#buttons-parameters
|
11
|
+
#
|
2
12
|
class Input
|
13
|
+
##
|
14
|
+
# A keyboard that can be attached to any message.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# go_somewhere = Viberroo::Input.url_button({
|
18
|
+
# Columns: 3,
|
19
|
+
# Rows: 2,
|
20
|
+
# Text: 'Mystery link',
|
21
|
+
# ActionBody: 'somewhere.com'
|
22
|
+
# })
|
23
|
+
#
|
24
|
+
# keyboard = Input.keyboard(Buttons: [go_somewhere])
|
25
|
+
#
|
26
|
+
# @see https://developers.viber.com/docs/tools/keyboards/#general-keyboard-parameters
|
27
|
+
#
|
3
28
|
def self.keyboard(params)
|
4
29
|
{ keyboard: { Type: 'keyboard' }.merge(params) }
|
5
30
|
end
|
6
31
|
|
32
|
+
##
|
33
|
+
# A reply button, when tapped sends it's body as a message.
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# button = Viberroo::Input.reply_button({
|
37
|
+
# Columns: 4,
|
38
|
+
# Rows: 3,
|
39
|
+
# ActionBody: '/search_cookies',
|
40
|
+
# Text: 'I want some cookies.'
|
41
|
+
# }
|
42
|
+
#
|
43
|
+
# @see https://developers.viber.com/docs/tools/keyboards/#buttons-parameters
|
44
|
+
#
|
7
45
|
def self.reply_button(params)
|
8
46
|
{ ActionType: 'reply' }.merge(params)
|
9
47
|
end
|
10
48
|
|
49
|
+
##
|
50
|
+
# A URL button, when tapped opens specified URL.
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
# button = Viberroo::Input.url_button({
|
54
|
+
# Columns: 4,
|
55
|
+
# Rows: 2,
|
56
|
+
# ActionBody: 'parrot.live',
|
57
|
+
# Text: 'Browse something weird'
|
58
|
+
# }
|
59
|
+
#
|
60
|
+
# @see https://developers.viber.com/docs/tools/keyboards/#buttons-parameters
|
61
|
+
#
|
11
62
|
def self.url_button(params)
|
12
63
|
{ ActionType: 'open-url' }.merge(params)
|
13
64
|
end
|
14
65
|
|
66
|
+
##
|
67
|
+
# @note Not supported on desktop.
|
68
|
+
#
|
69
|
+
# Location picker button, gives ability to pick a location on the map.
|
70
|
+
#
|
71
|
+
# @example
|
72
|
+
# button = Viberroo::Input.location_picker_button(location: { lat: 48.9215, lon: 24.7097 })
|
73
|
+
#
|
74
|
+
# @see https://developers.viber.com/docs/tools/keyboards/#buttons-parameters
|
75
|
+
#
|
15
76
|
def self.location_picker_button(params)
|
16
77
|
{ ActionType: 'location-picker',
|
17
78
|
min_api_version: 3
|
18
79
|
}.merge(params)
|
19
80
|
end
|
20
81
|
|
82
|
+
##
|
83
|
+
# @note Not supported on desktop.
|
84
|
+
#
|
85
|
+
# Share phone button.
|
86
|
+
#
|
87
|
+
# @example
|
88
|
+
# button = Viberroo::Input.share_phone_button(contact: { name: 'Gwythyr', phone_number: '12343214' })
|
89
|
+
#
|
90
|
+
# @see https://developers.viber.com/docs/tools/keyboards/#buttons-parameters
|
91
|
+
#
|
21
92
|
def self.share_phone_button(params)
|
22
93
|
{ ActionType: 'share-phone',
|
23
94
|
min_api_version: 3
|
24
95
|
}.merge(params)
|
25
96
|
end
|
26
97
|
|
98
|
+
##
|
99
|
+
# A button that does nothing, for decoration purposes.
|
100
|
+
#
|
101
|
+
# @example
|
102
|
+
# button = Viberroo::Input.none_button(Text: 'Purely decorative.')
|
103
|
+
#
|
104
|
+
# @see https://developers.viber.com/docs/tools/keyboards/#buttons-parameters
|
105
|
+
#
|
27
106
|
def self.none_button(params = {})
|
28
107
|
{ ActionType: 'none' }.merge(params)
|
29
108
|
end
|
data/lib/viberroo/message.rb
CHANGED
@@ -1,37 +1,191 @@
|
|
1
1
|
module Viberroo
|
2
|
+
##
|
3
|
+
# This class' methods serve as declarative wrappers with predefined types for
|
4
|
+
# each message type Viber API offers.
|
5
|
+
#
|
6
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#message-types
|
7
|
+
#
|
2
8
|
class Message
|
9
|
+
##
|
10
|
+
# Simple text message.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# message = Viberroo::Message.plain(text: 'Hello there!')
|
14
|
+
#
|
15
|
+
# @param [Hash] params
|
16
|
+
# @option params [String] text **Required**.
|
17
|
+
#
|
18
|
+
# @return [Hash]
|
19
|
+
#
|
20
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#text-message
|
21
|
+
#
|
3
22
|
def self.plain(params)
|
4
23
|
{ type: :text }.merge(params)
|
5
24
|
end
|
6
25
|
|
26
|
+
##
|
27
|
+
# The Rich Media message type allows sending messages with pre-defined layout,
|
28
|
+
# including height (rows number), width (columns number), text, images and buttons.
|
29
|
+
#
|
30
|
+
# @example Send a rich media
|
31
|
+
# search = Button.reply({
|
32
|
+
# Columns: 4,
|
33
|
+
# Rows: 3,
|
34
|
+
# ActionBody: '/search',
|
35
|
+
# Text: 'Search something...'
|
36
|
+
# }
|
37
|
+
#
|
38
|
+
# locate = Button.reply({
|
39
|
+
# Columns: 4,
|
40
|
+
# Rows: 3,
|
41
|
+
# ActionBody: '/near_me'
|
42
|
+
# }
|
43
|
+
#
|
44
|
+
# browse = Button.url({
|
45
|
+
# Columns: 4,
|
46
|
+
# Rows: 2,
|
47
|
+
# ActionBody: 'parrot.live',
|
48
|
+
# Text: 'Browse something wierd'
|
49
|
+
# }
|
50
|
+
#
|
51
|
+
# @bot.send_rich_media(
|
52
|
+
# rich_media: {
|
53
|
+
# ButtonsGroupColumns: 4,
|
54
|
+
# ButtonsGroupRows: 6,
|
55
|
+
# Buttons: [search, locate, browse]
|
56
|
+
# }
|
57
|
+
# )
|
58
|
+
#
|
59
|
+
# @param [Hash] params
|
60
|
+
# @option params [Hash] rich_media
|
61
|
+
# @option params [Integer] rich_media.ButtonsGroupColumns Number of columns per carousel content block. Possible values 1 - 6. **API Default**: 6.
|
62
|
+
# @option params [Integer] rich_media.ButtonsGroupRows Number of rows per carousel content block. Possible values 1 - 7. **API Default**: 7.
|
63
|
+
# @option params [Hash] rich_media.Buttons Array of buttons. Max of 6 * `ButtonsGroupColumns` * `ButtonsGroupRows`.
|
64
|
+
#
|
65
|
+
# @return [Hash]
|
66
|
+
#
|
67
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#rich-media-message--carousel-content-message
|
68
|
+
#
|
7
69
|
def self.rich(params)
|
8
70
|
{ type: :rich_media, min_api_version: 2 }.merge(params)
|
9
71
|
end
|
10
72
|
|
73
|
+
##
|
74
|
+
# Location message.
|
75
|
+
#
|
76
|
+
# @example
|
77
|
+
# message = Message.location(location: { lat: '48.9215', lon: '24.7097' })
|
78
|
+
#
|
79
|
+
# @param [Hash] params
|
80
|
+
# @option params [Hash] location
|
81
|
+
# @option params [Float] location.lat **Required**. Latitude
|
82
|
+
# @option params [Float] location.lon **Required**. Longitude
|
83
|
+
#
|
84
|
+
# @return [Hash]
|
85
|
+
#
|
86
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#location-message
|
87
|
+
#
|
11
88
|
def self.location(params)
|
12
89
|
{ type: :location }.merge(params)
|
13
90
|
end
|
14
91
|
|
92
|
+
##
|
93
|
+
# Picture message.
|
94
|
+
#
|
95
|
+
# @note Max image size: 1MB on iOS, 3MB on Android.
|
96
|
+
#
|
97
|
+
# @param [Hash] params
|
98
|
+
# @option params [String] media **Required**. Image URL. Allowed extensions: .jpeg, .png .gif. Animated GIFs can be sent as URL messages or file messages.
|
99
|
+
# @option params [String] text **Optional**. Max 120 characters.
|
100
|
+
# @option params [String] thumbnail **Optional**. Thumbnail URL. Max size 100 kb. Recommended: 400x400.
|
101
|
+
#
|
102
|
+
# @return [Hash]
|
103
|
+
#
|
104
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#picture-message
|
105
|
+
#
|
15
106
|
def self.picture(params = {})
|
16
107
|
{ type: :picture, text: '' }.merge(params)
|
17
108
|
end
|
18
109
|
|
110
|
+
##
|
111
|
+
# Video message.
|
112
|
+
#
|
113
|
+
# @note Max video size is 26MB.
|
114
|
+
#
|
115
|
+
# @param [Hash] params
|
116
|
+
# @option params [String] media **Required**. URL of the video (MP4, H264). Only MP4 and H264 are supported.
|
117
|
+
# @option params [Integer] size **Required**. Size of the video in bytes.
|
118
|
+
# @option params [Integer] duration **Optional**. Duration in seconds. Max value - 180.
|
119
|
+
# @option params [String] thumbnail **Optional**. Thumbnail URL. Max size 100 kb. Recommended: 400x400. Only JPEG format is supported.
|
120
|
+
#
|
121
|
+
# @return [Hash]
|
122
|
+
#
|
123
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#video-message
|
124
|
+
#
|
19
125
|
def self.video(params = {})
|
20
126
|
{ type: :video }.merge(params)
|
21
127
|
end
|
22
128
|
|
129
|
+
##
|
130
|
+
# File message.
|
131
|
+
#
|
132
|
+
# @note Max file size is 50MB.
|
133
|
+
#
|
134
|
+
# @param [Hash] params
|
135
|
+
# @option params [String] media **Required**. File URL.
|
136
|
+
# @option params [Integer] size **Required**. File size in bytes.
|
137
|
+
# @option params [String] file_name **Required**. Name of the file, should include extension. Max 256 characters (including extension).
|
138
|
+
#
|
139
|
+
# @return [Hash]
|
140
|
+
#
|
141
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#file-message
|
142
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#forbiddenFileFormats
|
143
|
+
#
|
23
144
|
def self.file(params = {})
|
24
145
|
{ type: :file }.merge(params)
|
25
146
|
end
|
26
147
|
|
148
|
+
##
|
149
|
+
# Contact message.
|
150
|
+
#
|
151
|
+
# @param [Hash] params
|
152
|
+
# @option params [Hash] contact
|
153
|
+
# @option params [Float] contact.name **Required**. Name of the contact. Max 28 characters.
|
154
|
+
# @option params [Float] contact.phone_number **Required**. Phone number of the contact. Max 18 characters.
|
155
|
+
#
|
156
|
+
# @return [Hash]
|
157
|
+
#
|
158
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#contact-message
|
159
|
+
#
|
27
160
|
def self.contact(params = {})
|
28
161
|
{ type: :contact }.merge(params)
|
29
162
|
end
|
30
163
|
|
164
|
+
##
|
165
|
+
# URL message.
|
166
|
+
#
|
167
|
+
# @param [Hash] params
|
168
|
+
# @option params [String] media **Required**. Max 2000 characters.
|
169
|
+
#
|
170
|
+
# @return [Hash]
|
171
|
+
#
|
172
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#url-message
|
173
|
+
#
|
31
174
|
def self.url(params = {})
|
32
175
|
{ type: :url }.merge(params)
|
33
176
|
end
|
34
177
|
|
178
|
+
##
|
179
|
+
# Sticker message.
|
180
|
+
#
|
181
|
+
# @param [Hash] params
|
182
|
+
# @option params [Integer] sticker_id **Required**. Max 2000 characters.
|
183
|
+
#
|
184
|
+
# @return [Hash]
|
185
|
+
#
|
186
|
+
# @see https://developers.viber.com/docs/api/rest-bot-api/#sticker-message
|
187
|
+
# @see https://developers.viber.com/docs/tools/sticker-ids/
|
188
|
+
#
|
35
189
|
def self.sticker(params = {})
|
36
190
|
{ type: :sticker }.merge(params)
|
37
191
|
end
|
data/lib/viberroo/response.rb
CHANGED
@@ -1,13 +1,40 @@
|
|
1
1
|
require 'recursive-open-struct'
|
2
2
|
|
3
3
|
module Viberroo
|
4
|
+
##
|
5
|
+
# Wraps callback response and provides helper methods for easier parameter
|
6
|
+
# access.
|
4
7
|
class Response
|
8
|
+
|
9
|
+
##
|
10
|
+
# Accessor for response parameters.
|
5
11
|
attr_reader :params
|
6
12
|
|
13
|
+
##
|
14
|
+
# @example
|
15
|
+
# class ViberController < ApplicationController
|
16
|
+
# skip_before_action :verify_authenticity_token
|
17
|
+
#
|
18
|
+
# def callback
|
19
|
+
# @response = Viberroo::Response.new(params.permit!)
|
20
|
+
# @bot = Viberroo::Bot.new(response: @response)
|
21
|
+
#
|
22
|
+
# head :ok
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @param [Hash] params Parameters from API callback.
|
27
|
+
#
|
7
28
|
def initialize(params)
|
8
|
-
@params = RecursiveOpenStruct.new
|
29
|
+
@params = RecursiveOpenStruct.new(params.to_h)
|
9
30
|
end
|
10
31
|
|
32
|
+
##
|
33
|
+
# Unifies user id access. Different callback events return user id differently.
|
34
|
+
# This method unifies user id access interface based on callback event type.
|
35
|
+
# Original user id params remain available in `params` attribute reader.
|
36
|
+
# @return [Integer || nil]
|
37
|
+
#
|
11
38
|
def user_id
|
12
39
|
case @params.event
|
13
40
|
when 'conversation_started', 'subscribed'
|
data/lib/viberroo/version.rb
CHANGED
data/lib/viberroo.rb
CHANGED
@@ -6,7 +6,13 @@ require 'viberroo/response'
|
|
6
6
|
require 'viberroo/bot'
|
7
7
|
require 'logger'
|
8
8
|
|
9
|
+
##
|
10
|
+
# Top namespace for all Viberroo code.
|
11
|
+
#
|
9
12
|
module Viberroo
|
13
|
+
##
|
14
|
+
# API endpoints.
|
15
|
+
#
|
10
16
|
module URL
|
11
17
|
API = 'https://chatapi.viber.com/pa'.freeze
|
12
18
|
WEBHOOK = "#{API}/set_webhook".freeze
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: viberroo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor Habchak
|
@@ -47,7 +47,7 @@ require_paths:
|
|
47
47
|
- lib
|
48
48
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
49
|
requirements:
|
50
|
-
- - "
|
50
|
+
- - ">="
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '2.3'
|
53
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
56
|
- !ruby/object:Gem::Version
|
57
57
|
version: '0'
|
58
58
|
requirements: []
|
59
|
-
rubygems_version: 3.
|
59
|
+
rubygems_version: 3.3.7
|
60
60
|
signing_key:
|
61
61
|
specification_version: 4
|
62
62
|
summary: Viber bot for Ruby / Rails.
|