Class: Viberroo::Bot
- Inherits:
-
Object
- Object
- Viberroo::Bot
- Defined in:
- lib/viberroo/bot.rb
Overview
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.
Instance Method Summary collapse
-
#broadcast(message:, to:) ⇒ Net::HTTPResponse || Hash
Broadcasts a messages to subscribed users by making a request to ‘/broadcast_message`.
-
#get_account_info ⇒ Net::HTTPResponse || Hash
Retrieves account info by making a request to ‘/get_account_info`.
-
#get_online(ids:) ⇒ Net::HTTPResponse || Hash
Retrieves a list of user status by making a request to ‘get_online`.
-
#get_user_details(id:) ⇒ Net::HTTPResponse || Hash
Retrieves details of particular user by making a request to ‘/get_user_details`.
-
#initialize(response: Response.new({}), token: nil) ⇒ Bot
constructor
A new instance of Bot.
-
#remove_webhook ⇒ Net::HTTPResponse || Hash
Removes a webhook by making a request to ‘/set_webhook`.
-
#send(message:, keyboard: {}) ⇒ Object
deprecated
Deprecated.
Use #send_message instead.
-
#send_message(message, keyboard: {}) ⇒ Net::HTTPResponse || Hash
Sends a message to a user by making a request to ‘/send_message`.
-
#set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil) ⇒ Net::HTTPResponse || Hash
Sets a webhook by making a request to ‘/set_webhook`.
Constructor Details
#initialize(response: Response.new({}), token: nil) ⇒ Bot
Returns a new instance of Bot.
31 32 33 34 35 36 37 38 39 |
# File 'lib/viberroo/bot.rb', line 31 def initialize(response: Response.new({}), token: nil) Viberroo.configure @headers = { 'X-Viber-Auth-Token': token || Viberroo.config.auth_token, 'Content-Type': 'application/json' } @response = response end |
Instance Method Details
#broadcast(message:, to:) ⇒ Net::HTTPResponse || Hash
This request has a rate limit of 500 requests in a 10 seconds window.
Broadcasts a messages to subscribed users by making a request to ‘/broadcast_message`.
180 181 182 |
# File 'lib/viberroo/bot.rb', line 180 def broadcast(message:, to:) request(URL::BROADCAST_MESSAGE, .merge(broadcast_list: to)) end |
#get_account_info ⇒ Net::HTTPResponse || Hash
Retrieves account info by making a request to ‘/get_account_info`. These settings can be set in you Viber admin panel.
191 192 193 |
# File 'lib/viberroo/bot.rb', line 191 def get_account_info request(URL::GET_ACCOUNT_INFO) end |
#get_online(ids:) ⇒ Net::HTTPResponse || Hash
The API supports up to 100 user id per request and those users must be subscribed to the account.
Retrieves a list of user status by making a request to ‘get_online`.
227 228 229 |
# File 'lib/viberroo/bot.rb', line 227 def get_online(ids:) request(URL::GET_ONLINE, ids: ids) end |
#get_user_details(id:) ⇒ Net::HTTPResponse || Hash
This request can be sent twice during a 12 hours period for each user ID.
Retrieves details of particular user by making a request to ‘/get_user_details`.
208 209 210 |
# File 'lib/viberroo/bot.rb', line 208 def get_user_details(id:) request(URL::GET_USER_DETAILS, id: id) end |
#remove_webhook ⇒ Net::HTTPResponse || Hash
Removes a webhook by making a request to ‘/set_webhook`.
85 86 87 |
# File 'lib/viberroo/bot.rb', line 85 def remove_webhook request(URL::WEBHOOK, url: '') end |
#send(message:, keyboard: {}) ⇒ Object
Use #send_message instead.
153 154 155 156 157 158 159 160 |
# File 'lib/viberroo/bot.rb', line 153 def send(message:, keyboard: {}) Viberroo.config.logger.info(<<~WARNING) DEPRECATION WARNING: Bot#send method is going to be removed in the next minor release. Use Bot#send_message instead. WARNING (, keyboard: keyboard) end |
#send_message(message, keyboard: {}) ⇒ Net::HTTPResponse || Hash
Sends a message to a user by making a request to ‘/send_message`.
143 144 145 146 147 148 149 150 |
# File 'lib/viberroo/bot.rb', line 143 def (, keyboard: {}) request( URL::MESSAGE, { receiver: @response.user_id }.merge(, keyboard).tap do |hash| hash.merge!(min_api_version_hash(keyboard)) unless hash[:min_api_version] end ) end |
#set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil) ⇒ Net::HTTPResponse || Hash
Sets a webhook by making a request to ‘/set_webhook`. Necessary for receiving callbacks from Viber. 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.
66 67 68 69 |
# File 'lib/viberroo/bot.rb', line 66 def set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil) request(URL::WEBHOOK, url: url, event_types: event_types, send_name: send_name, send_photo: send_photo) end |