viberroo 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/viberroo.rb +3 -3
- data/lib/viberroo/bot.rb +38 -40
- data/lib/viberroo/configuration.rb +38 -0
- data/lib/viberroo/{callback.rb → response.rb} +1 -1
- data/lib/viberroo/version.rb +1 -1
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfdd2b9cf561f8fe620e7e8fd58c646aded156836519095de599f794022c5771
|
4
|
+
data.tar.gz: 394fcb5010e801f8595c2fe33cb5f28d72e95d961a7cf13cb33b8d006dc59a86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 155014f7f40d8730f9ad41c9193dd739caa33b906587e0ff4ba6ed2fe7e3890841c12eff617ec7951cceaecca1923cbb59f5a6eecb24a9bf7ff3a664a4f582ac
|
7
|
+
data.tar.gz: 7baf397be2823482dc211c0c3392f3c8bedf981da346e06dc9dae91d41e27db0ce7feb5afdf4d151a65e67bdf9d3a003389cc7ee077123d9dc7f371efa0ee4df
|
data/lib/viberroo.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'json'
|
2
|
-
require '
|
2
|
+
require 'viberroo/configuration'
|
3
3
|
require 'viberroo/message'
|
4
|
-
require 'viberroo/bot'
|
5
4
|
require 'viberroo/input'
|
6
|
-
require 'viberroo/
|
5
|
+
require 'viberroo/response'
|
7
6
|
require 'viberroo/bot'
|
7
|
+
require 'logger'
|
8
8
|
|
9
9
|
module Viberroo
|
10
10
|
module URL
|
data/lib/viberroo/bot.rb
CHANGED
@@ -1,83 +1,81 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
# Namespace for all Viberroo code.
|
1
5
|
module Viberroo
|
6
|
+
|
7
|
+
# Used for sending requests to Viber API. Each request sends a http POST request to a particular endpoint, each returns http response.
|
2
8
|
class Bot
|
3
|
-
def initialize(token
|
4
|
-
|
5
|
-
|
9
|
+
def initialize(token: nil, response: nil)
|
10
|
+
Viberroo.configure
|
11
|
+
|
12
|
+
@headers = {
|
13
|
+
'X-Viber-Auth-Token': token || Viberroo.config.auth_token,
|
14
|
+
'Content-Type': 'application/json'
|
15
|
+
}
|
16
|
+
@response = response
|
6
17
|
end
|
7
18
|
|
19
|
+
# Used to set bot webhook.
|
8
20
|
def set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil)
|
9
21
|
request(URL::WEBHOOK, url: url, event_types: event_types,
|
10
|
-
send_name: send_name,
|
11
|
-
send_photo: send_photo)
|
12
|
-
end
|
13
|
-
|
14
|
-
def set_webhook!(url:, event_types: nil, send_name: nil, send_photo: nil)
|
15
|
-
parse set_webhook(url: url, event_types: event_types,
|
16
|
-
send_name: send_name,
|
17
|
-
send_photo: send_photo)
|
22
|
+
send_name: send_name, send_photo: send_photo)
|
18
23
|
end
|
19
24
|
|
25
|
+
# Used to remove bot webhook.
|
20
26
|
def remove_webhook
|
21
27
|
request(URL::WEBHOOK, url: '')
|
22
28
|
end
|
23
29
|
|
24
|
-
|
25
|
-
parse remove_webhook
|
26
|
-
end
|
27
|
-
|
30
|
+
# Used to send messages.
|
28
31
|
def send(message:, keyboard: {})
|
29
32
|
request(URL::MESSAGE,
|
30
|
-
{ receiver: @
|
31
|
-
end
|
32
|
-
|
33
|
-
def send!(message:, keyboard: {})
|
34
|
-
parse self.send(message: message, keyboard: keyboard)
|
33
|
+
{ receiver: @response&.user_id }.merge(message).merge(keyboard))
|
35
34
|
end
|
36
35
|
|
36
|
+
# Used to broadcast messages.
|
37
37
|
def broadcast(message:, to:)
|
38
38
|
request(URL::BROADCAST_MESSAGE, message.merge(broadcast_list: to))
|
39
39
|
end
|
40
40
|
|
41
|
-
|
42
|
-
parse broadcast(message: message, to: to)
|
43
|
-
end
|
44
|
-
|
41
|
+
# Used to retrieve account info. These settings can be set in you Viber admin panel.
|
45
42
|
def get_account_info
|
46
43
|
request(URL::GET_ACCOUNT_INFO)
|
47
44
|
end
|
48
45
|
|
49
|
-
|
50
|
-
parse get_account_info
|
51
|
-
end
|
52
|
-
|
46
|
+
# Used to retrieve details of a particular user.
|
53
47
|
def get_user_details(id:)
|
54
48
|
request(URL::GET_USER_DETAILS, id: id)
|
55
49
|
end
|
56
50
|
|
57
|
-
|
58
|
-
parse get_user_details(id: id)
|
59
|
-
end
|
60
|
-
|
51
|
+
# Used to get online of a list of subscribed users.
|
61
52
|
def get_online(ids:)
|
62
53
|
request(URL::GET_ONLINE, ids: ids)
|
63
54
|
end
|
64
55
|
|
65
|
-
def get_online!(ids:)
|
66
|
-
parse get_online(ids: ids)
|
67
|
-
end
|
68
|
-
|
69
56
|
private
|
70
57
|
|
71
58
|
def request(url, params = {})
|
72
|
-
|
73
|
-
end
|
59
|
+
uri = URI(url)
|
74
60
|
|
75
|
-
|
76
|
-
|
61
|
+
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
62
|
+
request = Net::HTTP::Post.new(uri, @headers)
|
63
|
+
request.body = compact(params).to_json
|
64
|
+
|
65
|
+
http.request(request)
|
66
|
+
end
|
67
|
+
|
68
|
+
Viberroo.config.logger&.info("##{caller_name} -- #{response.body}")
|
69
|
+
|
70
|
+
Viberroo.config.parse_response_body ? JSON.parse(response.body) : response
|
77
71
|
end
|
78
72
|
|
79
73
|
def compact(params)
|
80
74
|
params.delete_if { |_, v| v.nil? }
|
81
75
|
end
|
76
|
+
|
77
|
+
def caller_name
|
78
|
+
caller[1][/`.*'/][1..-2]
|
79
|
+
end
|
82
80
|
end
|
83
81
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Viberroo
|
2
|
+
class << self
|
3
|
+
# Accessor for global configuration.
|
4
|
+
attr_accessor :config
|
5
|
+
end
|
6
|
+
|
7
|
+
# Yields the global configuration to a block. Returns existing configuration if one was defined earlier.
|
8
|
+
# @yield [Configuration] global configuration
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# RSpec.configure do |config|
|
12
|
+
# config.auth_token = 'syEsp8e0'
|
13
|
+
# end
|
14
|
+
# @see Viberroo::Configuration
|
15
|
+
def self.configure
|
16
|
+
self.config ||= Configuration.new
|
17
|
+
yield(config) if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
# Stores runtime configuration information.
|
21
|
+
class Configuration
|
22
|
+
attr_accessor :logger, :auth_token, :parse_response_body
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
# Stores Viber API authentication token. Necessary for the bot to send API requests.
|
26
|
+
@auth_token = nil
|
27
|
+
|
28
|
+
# Specifies logger.
|
29
|
+
@logger = Logger.new(STDOUT)
|
30
|
+
@logger.formatter = proc do |severity, datetime, _, msg|
|
31
|
+
"[#{datetime}] #{severity} Viberroo::Bot #{msg}\n"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Specifies whether to parse request response body.
|
35
|
+
@parse_response_body = true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/viberroo/version.rb
CHANGED
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.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viktor Habchak
|
@@ -10,20 +10,6 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2020-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: faraday
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 1.0.0
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 1.0.0
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: recursive-open-struct
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -46,9 +32,10 @@ extra_rdoc_files: []
|
|
46
32
|
files:
|
47
33
|
- lib/viberroo.rb
|
48
34
|
- lib/viberroo/bot.rb
|
49
|
-
- lib/viberroo/
|
35
|
+
- lib/viberroo/configuration.rb
|
50
36
|
- lib/viberroo/input.rb
|
51
37
|
- lib/viberroo/message.rb
|
38
|
+
- lib/viberroo/response.rb
|
52
39
|
- lib/viberroo/version.rb
|
53
40
|
homepage: https://github.com/vikdotdev/viberroo
|
54
41
|
licenses:
|
@@ -72,5 +59,5 @@ requirements: []
|
|
72
59
|
rubygems_version: 3.0.3
|
73
60
|
signing_key:
|
74
61
|
specification_version: 4
|
75
|
-
summary: Viber bot for Ruby.
|
62
|
+
summary: Viber bot for Ruby / Rails.
|
76
63
|
test_files: []
|