viberroo 0.0.7 → 0.1.2

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: ffd5dd8c29342acd993ed001e8957b006e7e74e82ee7f64f887b9fe7dd4cb42c
4
- data.tar.gz: f1febbae3bf056dd257c726f794f1124efc3f20ac41014dbddb95ade90408d54
3
+ metadata.gz: 664a309e7aaa7c6aef70d20e94145949bb6c4ff6753f15910440bae46b2549a4
4
+ data.tar.gz: 33a67d36213f503ba8bfc425dfbd1f69bc824fb7d2a01ad4d5e5e535727fe710
5
5
  SHA512:
6
- metadata.gz: 1e9ffcebd82b968bb287484553170dbce430b036c9580fedec698540ab63738bb1c5b187d46765c5f5e96ab1b39151efaffcdb4625050157c9d429f1085944ab
7
- data.tar.gz: cc2ca4deb311c1cce8b294f4fdcb3be3c4e67b2ce3a7818ca2afb6b9847764d51a8c7df6c3b5c3c6915a31712f884cd44a5326488c8c1faeef2d03f2a3b757da
6
+ metadata.gz: 134a5e4b9ed4d2b8fdd057874c8c4238803245173542b936582604c8ec1dfd5bff3251ff694d1a7dc75272129dd9b1ef2e558d517939aac2c2bd2b3a8eb86770
7
+ data.tar.gz: bebeab8ecf509af9b3d26a5e708fd22c03783ea887a7d95bca1c258ccab9fdb1dfa43766569e9423f93d7731e383436b88aa771363eeb4c5c45d5583ca2be410
@@ -4,8 +4,17 @@ require 'viberroo/message'
4
4
  require 'viberroo/bot'
5
5
  require 'viberroo/input'
6
6
  require 'viberroo/response'
7
+ require 'viberroo/callback'
7
8
  require 'viberroo/bot'
8
9
 
9
10
  module Viberroo
10
- API_URL = 'https://chatapi.viber.com/pa'.freeze
11
+ module URL
12
+ API = 'https://chatapi.viber.com/pa'.freeze
13
+ WEBHOOK = "#{API}/set_webhook".freeze
14
+ MESSAGE = "#{API}/send_message".freeze
15
+ BROADCAST_MESSAGE = "#{API}/broadcast_message".freeze
16
+ GET_ACCOUNT_INFO = "#{API}/get_account_info".freeze
17
+ GET_USER_DETAILS = "#{API}/get_user_details".freeze
18
+ GET_ONLINE = "#{API}/get_online".freeze
19
+ end
11
20
  end
@@ -1,16 +1,91 @@
1
+ require 'byebug'
1
2
  module Viberroo
2
3
  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"
4
+ def initialize(token:, callback: {}, response: {})
8
5
  @headers = { 'X-Viber-Auth-Token': token, 'Content-Type': 'application/json' }
9
6
  @response = response
7
+ @callback = callback
8
+ end
9
+
10
+ def set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil)
11
+ request(URL::WEBHOOK, url: url, event_types: event_types,
12
+ send_name: send_name,
13
+ send_photo: send_photo)
14
+ end
15
+
16
+ def set_webhook!(url:, event_types: nil, send_name: nil, send_photo: nil)
17
+ parse set_webhook(url: url, event_types: event_types,
18
+ send_name: send_name,
19
+ send_photo: send_photo)
20
+ end
21
+
22
+ def remove_webhook
23
+ request(URL::WEBHOOK, url: '')
24
+ end
25
+
26
+ def remove_webhook!
27
+ parse remove_webhook
28
+ end
29
+
30
+ def send(message:, keyboard: {})
31
+ if @callback.is_a?(Callback)
32
+ receiver = @callback.user_id
33
+ elsif @response.is_a?(RecursiveOpenStruct)
34
+ receiver = @response.user_id
35
+ end
36
+
37
+ request(URL::MESSAGE,
38
+ { receiver: receiver }.merge(message).merge(keyboard))
39
+ end
40
+
41
+ def send!(message:, keyboard: {})
42
+ parse self.send(message: message, keyboard: keyboard)
43
+ end
44
+
45
+ def broadcast(message:, to:)
46
+ request(URL::BROADCAST_MESSAGE, message.merge(broadcast_list: to))
47
+ end
48
+
49
+ def broadcast!(message:, to:)
50
+ parse broadcast(message: message, to: to)
51
+ end
52
+
53
+ def get_account_info
54
+ request(URL::GET_ACCOUNT_INFO)
55
+ end
56
+
57
+ def get_account_info!
58
+ parse get_account_info
59
+ end
60
+
61
+ def get_user_details(id:)
62
+ request(URL::GET_USER_DETAILS, id: id)
63
+ end
64
+
65
+ def get_user_details!(id:)
66
+ parse get_user_details(id: id)
67
+ end
68
+
69
+ def get_online(ids:)
70
+ request(URL::GET_ONLINE, ids: ids)
71
+ end
72
+
73
+ def get_online!(ids:)
74
+ parse get_online(ids: ids)
75
+ end
76
+
77
+ private
78
+
79
+ def request(url, params = {})
80
+ Faraday.post(url, compact(params).to_json, @headers)
81
+ end
82
+
83
+ def parse(request)
84
+ JSON.parse(request.body)
10
85
  end
11
86
 
12
- def set_webhook(params)
13
- Faraday.post(@webhook_url, params.to_json, @headers)
87
+ def compact(params)
88
+ params.delete_if { |_, v| v.nil? }
14
89
  end
15
90
  end
16
91
  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,65 +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
- # required params = text
7
- def send_message(params = {})
8
- dispatch({ type: :text }.merge(params))
9
- end
10
-
11
- def send_rich_media(params = {})
12
- dispatch({ type: :rich_media, min_api_version: 2 }.merge(params))
2
+ class Message
3
+ def self.plain(params)
4
+ { type: :text }.merge(params)
13
5
  end
14
6
 
15
- # required params = location.lat, location.lon
16
- def send_location(params = {})
17
- dispatch({ type: :location, min_api_version: 3 }.merge(params))
7
+ def self.rich(params)
8
+ { type: :rich_media, min_api_version: 2 }.merge(params)
18
9
  end
19
10
 
20
- # required params = text, media
21
- def send_picture(params = {})
22
- dispatch({ type: :picture }.merge(params))
11
+ def self.location(params)
12
+ { type: :location }.merge(params)
23
13
  end
24
14
 
25
- # required params = text, media
26
- def send_video(params = {})
27
- dispatch({ type: :video }.merge(params))
15
+ def self.picture(params = {})
16
+ { type: :picture, text: '' }.merge(params)
28
17
  end
29
18
 
30
- # required params = media, size, file_name
31
- def send_file(params = {})
32
- dispatch({ type: :file }.merge(params))
19
+ def self.video(params = {})
20
+ { type: :video }.merge(params)
33
21
  end
34
22
 
35
- # required params = contact, contact.name, contact.phone_number
36
- def send_contact(params = {})
37
- dispatch({ type: :contact }.merge(params))
23
+ def self.file(params = {})
24
+ { type: :file }.merge(params)
38
25
  end
39
26
 
40
- # required params = media
41
- def send_url(params = {})
42
- dispatch({ type: :url }.merge(params))
27
+ def self.contact(params = {})
28
+ { type: :contact }.merge(params)
43
29
  end
44
30
 
45
- # required params = sticker_id
46
- # https://viber.github.io/docs/tools/sticker-ids/
47
- def send_sticker(params = {})
48
- dispatch({ type: :sticker }.merge(params))
49
- end
50
-
51
- private
52
-
53
- def dispatch(params)
54
- Faraday.post(
55
- @message_url,
56
- normalize({ receiver: @response.user_id }.merge(params)),
57
- @headers
58
- )
31
+ def self.url(params = {})
32
+ { type: :url }.merge(params)
59
33
  end
60
34
 
61
- def normalize(params)
62
- params.delete_if { |_, v| v.nil? }.to_json
35
+ def self.sticker(params = {})
36
+ { type: :sticker }.merge(params)
63
37
  end
64
38
  end
65
39
  end
@@ -4,6 +4,7 @@ module Viberroo
4
4
  class Response
5
5
  class << self
6
6
  def init(params)
7
+ warn 'WARNING: Response.init is deprecated and will be gone in next minor release. Use Callback.new as a replacement.'
7
8
  @@os = RecursiveOpenStruct.new params.to_h
8
9
  @@os.user_id = user_id
9
10
  @@os
@@ -19,6 +20,8 @@ module Viberroo
19
20
  @@os.user_id
20
21
  when 'message'
21
22
  @@os.sender.id
23
+ else
24
+ @@os.dig(:user, :id)
22
25
  end
23
26
  end
24
27
  end
@@ -1,3 +1,3 @@
1
1
  module Viberroo
2
- VERSION = '0.0.7'.freeze
2
+ VERSION = '0.1.2'.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.7
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor Habchak
@@ -69,8 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
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: []