viberroo 0.0.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78bcb0e32349405f96129347dc9cbca08f1af7b3fc15b0bdc93aef2199c76e1e
4
- data.tar.gz: 98d3a81828703929aead8b437bfd3aab024c43491d2d6bf1c087f8fc9f6ff1c1
3
+ metadata.gz: 6e99b6a05f07fc41a2ae9952fe3d5088b34f55ce541f5624c9257713e29ec0c4
4
+ data.tar.gz: 432960ff25d67e609e5357b76a4b63c33115aef892016b964847ee6fa980be5c
5
5
  SHA512:
6
- metadata.gz: d4e6e9eba4b27eb6442800440f8849ec957528753fbcee168dbd1c8e0cb0ad62d4ed51173ad5b5be0aeb567dbab9cd4d4e504ebb38a1bfcbeb55d9267b152228
7
- data.tar.gz: 07d4b7a5b117c958fc6af4eef204283b7af93c62fb14de25cc5f76019b9f59a8f5280bd3ec581c3dbed84ca224a4c7f81a63ee60735bdc9ee9784eae1bed3bed
6
+ metadata.gz: 4f800f461911f603c1a95ca30cfbe5ec0b33b9c1c02df3e681c462a2c1307fd7014074bd95f9478f2ee44c6c061a13c6ecba6533eee35c8d5d074c612425c076
7
+ data.tar.gz: 78ea9bf73786aa9d36b1c43606b7df8363575237813f4e954bb980a7e3d188602f6db088eab3d05c1a48f3708d2588e2d110d55a42fae18f348efbcaca55649c
@@ -3,9 +3,17 @@ require 'faraday'
3
3
  require 'viberroo/message'
4
4
  require 'viberroo/bot'
5
5
  require 'viberroo/input'
6
- require 'viberroo/response'
6
+ require 'viberroo/callback'
7
7
  require 'viberroo/bot'
8
8
 
9
9
  module Viberroo
10
- API_URL = 'https://chatapi.viber.com/pa'.freeze
10
+ module URL
11
+ API = 'https://chatapi.viber.com/pa'.freeze
12
+ WEBHOOK = "#{API}/set_webhook".freeze
13
+ MESSAGE = "#{API}/send_message".freeze
14
+ BROADCAST_MESSAGE = "#{API}/broadcast_message".freeze
15
+ GET_ACCOUNT_INFO = "#{API}/get_account_info".freeze
16
+ GET_USER_DETAILS = "#{API}/get_user_details".freeze
17
+ GET_ONLINE = "#{API}/get_online".freeze
18
+ end
11
19
  end
@@ -1,20 +1,83 @@
1
1
  module Viberroo
2
2
  class Bot
3
- include Message
4
-
5
- def initialize(token:, response: {})
6
- @webhook_url = "#{API_URL}/set_webhook"
7
- @message_url = "#{API_URL}/send_message"
3
+ def initialize(token:, callback: nil)
8
4
  @headers = { 'X-Viber-Auth-Token': token, 'Content-Type': 'application/json' }
9
- @response = response
5
+ @callback = callback
6
+ end
7
+
8
+ def set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil)
9
+ request(URL::WEBHOOK, url: url, event_types: event_types,
10
+ send_name: send_name,
11
+ send_photo: send_photo)
10
12
  end
11
13
 
12
- def set_webhook(params)
13
- Faraday.post(@webhook_url, params.to_json, @headers)
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)
14
18
  end
15
19
 
16
20
  def remove_webhook
17
- Faraday.post(@webhook_url, { url: '' }.to_json, @headers)
21
+ request(URL::WEBHOOK, url: '')
22
+ end
23
+
24
+ def remove_webhook!
25
+ parse remove_webhook
26
+ end
27
+
28
+ def send(message:, keyboard: {})
29
+ request(URL::MESSAGE,
30
+ { receiver: @callback&.user_id }.merge(message).merge(keyboard))
31
+ end
32
+
33
+ def send!(message:, keyboard: {})
34
+ parse self.send(message: message, keyboard: keyboard)
35
+ end
36
+
37
+ def broadcast(message:, to:)
38
+ request(URL::BROADCAST_MESSAGE, message.merge(broadcast_list: to))
39
+ end
40
+
41
+ def broadcast!(message:, to:)
42
+ parse broadcast(message: message, to: to)
43
+ end
44
+
45
+ def get_account_info
46
+ request(URL::GET_ACCOUNT_INFO)
47
+ end
48
+
49
+ def get_account_info!
50
+ parse get_account_info
51
+ end
52
+
53
+ def get_user_details(id:)
54
+ request(URL::GET_USER_DETAILS, id: id)
55
+ end
56
+
57
+ def get_user_details!(id:)
58
+ parse get_user_details(id: id)
59
+ end
60
+
61
+ def get_online(ids:)
62
+ request(URL::GET_ONLINE, ids: ids)
63
+ end
64
+
65
+ def get_online!(ids:)
66
+ parse get_online(ids: ids)
67
+ end
68
+
69
+ private
70
+
71
+ def request(url, params = {})
72
+ Faraday.post(url, compact(params).to_json, @headers)
73
+ end
74
+
75
+ def parse(request)
76
+ JSON.parse(request.body)
77
+ end
78
+
79
+ def compact(params)
80
+ params.delete_if { |_, v| v.nil? }
18
81
  end
19
82
  end
20
83
  end
@@ -0,0 +1,24 @@
1
+ require 'recursive-open-struct'
2
+
3
+ module Viberroo
4
+ class Callback
5
+ attr_reader :params
6
+
7
+ def initialize(params)
8
+ @params = RecursiveOpenStruct.new params.to_h
9
+ end
10
+
11
+ def user_id
12
+ case @params.event
13
+ when 'conversation_started', 'subscribed'
14
+ @params.user.id
15
+ when 'unsubscribed', 'delivered', 'seen', 'failed'
16
+ @params.user_id
17
+ when 'message'
18
+ @params.sender.id
19
+ else
20
+ @params.dig(:user, :id)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,33 +1,31 @@
1
1
  module Viberroo
2
- module Input
3
- class Button
4
- def self.reply(params = {})
5
- { ActionType: 'reply' }.merge(params)
6
- end
2
+ class Input
3
+ def self.keyboard(params)
4
+ { keyboard: { Type: 'keyboard' }.merge(params) }
5
+ end
7
6
 
8
- def self.url(params = {})
9
- { ActionType: 'open-url' }.merge(params)
10
- end
7
+ def self.reply_button(params)
8
+ { ActionType: 'reply' }.merge(params)
9
+ end
11
10
 
12
- def self.location_picker(params = {})
13
- { ActionType: 'location-picker',
14
- min_api_version: 3
15
- }.merge(params)
16
- end
11
+ def self.url_button(params)
12
+ { ActionType: 'open-url' }.merge(params)
13
+ end
17
14
 
18
- def self.share_phone(params = {})
19
- { ActionType: 'share-phone',
20
- min_api_version: 3
21
- }.merge(params)
22
- end
15
+ def self.location_picker_button(params)
16
+ { ActionType: 'location-picker',
17
+ min_api_version: 3
18
+ }.merge(params)
19
+ end
23
20
 
24
- def self.none(params = {})
25
- { ActionType: 'none' }.merge(params)
26
- end
21
+ def self.share_phone_button(params)
22
+ { ActionType: 'share-phone',
23
+ min_api_version: 3
24
+ }.merge(params)
27
25
  end
28
26
 
29
- def keyboard(params = {})
30
- { Type: 'keyboard' }.merge(params)
27
+ def self.none_button(params = {})
28
+ { ActionType: 'none' }.merge(params)
31
29
  end
32
30
  end
33
31
  end
@@ -1,57 +1,39 @@
1
1
  module Viberroo
2
- module Message
3
- # TODO: add list of params that is mandatory for particular message type,
4
- # throw exceptions and log errors if not found
5
-
6
- def send_message(params = {})
7
- dispatch({ type: :text }.merge(params))
8
- end
9
-
10
- def send_rich_media(params = {})
11
- dispatch({ type: :rich_media, min_api_version: 2 }.merge(params))
2
+ class Message
3
+ def self.plain(params)
4
+ { type: :text }.merge(params)
12
5
  end
13
6
 
14
- def send_location(params = {})
15
- dispatch({ type: :location, min_api_version: 3 }.merge(params))
7
+ def self.rich(params)
8
+ { type: :rich_media, min_api_version: 2 }.merge(params)
16
9
  end
17
10
 
18
- def send_picture(params = {})
19
- dispatch({ type: :picture }.merge(params))
11
+ def self.location(params)
12
+ { type: :location }.merge(params)
20
13
  end
21
14
 
22
- def send_video(params = {})
23
- dispatch({ type: :video }.merge(params))
15
+ def self.picture(params = {})
16
+ { type: :picture, text: '' }.merge(params)
24
17
  end
25
18
 
26
- def send_file(params = {})
27
- dispatch({ type: :file }.merge(params))
19
+ def self.video(params = {})
20
+ { type: :video }.merge(params)
28
21
  end
29
22
 
30
- def send_contact(params = {})
31
- dispatch({ type: :contact }.merge(params))
23
+ def self.file(params = {})
24
+ { type: :file }.merge(params)
32
25
  end
33
26
 
34
- def send_url(params = {})
35
- dispatch({ type: :url }.merge(params))
27
+ def self.contact(params = {})
28
+ { type: :contact }.merge(params)
36
29
  end
37
30
 
38
- # https://viber.github.io/docs/tools/sticker-ids/
39
- def send_sticker(params = {})
40
- dispatch({ type: :sticker }.merge(params))
41
- end
42
-
43
- private
44
-
45
- def dispatch(params)
46
- Faraday.post(
47
- @message_url,
48
- normalize({ receiver: @response.user_id }.merge(params)),
49
- @headers
50
- )
31
+ def self.url(params = {})
32
+ { type: :url }.merge(params)
51
33
  end
52
34
 
53
- def normalize(params)
54
- params.delete_if { |_, v| v.nil? }.to_json
35
+ def self.sticker(params = {})
36
+ { type: :sticker }.merge(params)
55
37
  end
56
38
  end
57
39
  end
@@ -1,3 +1,3 @@
1
1
  module Viberroo
2
- VERSION = '0.0.8'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.0.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor Habchak
@@ -46,9 +46,9 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - lib/viberroo.rb
48
48
  - lib/viberroo/bot.rb
49
+ - lib/viberroo/callback.rb
49
50
  - lib/viberroo/input.rb
50
51
  - lib/viberroo/message.rb
51
- - lib/viberroo/response.rb
52
52
  - lib/viberroo/version.rb
53
53
  homepage: https://github.com/vikdotdev/viberroo
54
54
  licenses:
@@ -60,17 +60,17 @@ require_paths:
60
60
  - lib
61
61
  required_ruby_version: !ruby/object:Gem::Requirement
62
62
  requirements:
63
- - - ">="
63
+ - - "~>"
64
64
  - !ruby/object:Gem::Version
65
- version: '0'
65
+ version: '2.3'
66
66
  required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  requirements:
68
68
  - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.1.2
72
+ rubygems_version: 3.0.3
73
73
  signing_key:
74
74
  specification_version: 4
75
- summary: Thin Viber REST API wrapper for Ruby.
75
+ summary: Viber bot for Ruby.
76
76
  test_files: []
@@ -1,26 +0,0 @@
1
- require 'recursive-open-struct'
2
-
3
- module Viberroo
4
- class Response
5
- class << self
6
- def init(params)
7
- @@os = RecursiveOpenStruct.new params.to_h
8
- @@os.user_id = user_id
9
- @@os
10
- end
11
-
12
- private
13
-
14
- def user_id
15
- case @@os.event
16
- when 'conversation_started', 'subscribed'
17
- @@os.user.id
18
- when 'unsubscribed', 'delivered', 'seen', 'failed'
19
- @@os.user_id
20
- when 'message'
21
- @@os.sender.id
22
- end
23
- end
24
- end
25
- end
26
- end