viberroo 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7469da3b388fff29149846d0bd74dcc4edf1504b8d483b07ca41f76874ad1d0
4
- data.tar.gz: f6a35359dda32689c7782f2185eab4dc6fa1adb6819c35b30c2f31eeb641b018
3
+ metadata.gz: c30b02222199f1c17401145ec5684fe268cfe927466075d599c6b92740a5fd27
4
+ data.tar.gz: c929a33404dda4194f6d6b4cf4d05e8f9d70c8069c6ba5e56ff7ddf7b01a8249
5
5
  SHA512:
6
- metadata.gz: 85fe34b50a9047820618f818786be6620f022f89ccbabfdaae85ba3335bcbee3b4a98eaffab74fc598f4e124b31d97539a4c1d3106eca890bfd3c6a0da020c1e
7
- data.tar.gz: 2d20dcab05ac4917167a308bdbd01cf7bca380fe355aea41c1cad599457f844ed0a149ecaff986fb70f4f34bf3751eca06b8d285a47a84f779fe6769305fa22c
6
+ metadata.gz: d9d732ef550ec85681e0f3b22ba408d3c531bba37c518d862dff6c8abc82c973e55e84d0dc2844cadfc5223186ef9bb0b018ced6b6ddf65c54c8cafb94922f90
7
+ data.tar.gz: 49cdbc99c87e1178c7e020d4ba9c755a5401fbd798d244b9bf2649eae4d5867fa353c621285b3e7f1f17886cf54095474b3c77d5b1f11d377af5bd7bedbee376
@@ -7,5 +7,13 @@ require 'viberroo/response'
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,55 @@
1
1
  module Viberroo
2
2
  class Bot
3
- include Message
4
-
5
3
  def initialize(token:, response: {})
6
- @webhook_url = "#{API_URL}/set_webhook"
7
- @message_url = "#{API_URL}/send_message"
8
4
  @headers = { 'X-Viber-Auth-Token': token, 'Content-Type': 'application/json' }
9
5
  @response = response
10
6
  end
11
7
 
12
- def set_webhook(params)
13
- Faraday.post(@webhook_url, params.to_json, @headers)
8
+ def set_webhook(url:, event_types: nil, send_name: nil, send_photo: nil)
9
+ request(
10
+ URL::WEBHOOK,
11
+ url: url,
12
+ event_types: event_types,
13
+ send_name: send_name,
14
+ send_photo: send_photo
15
+ )
14
16
  end
15
17
 
16
18
  def remove_webhook
17
- Faraday.post(@webhook_url, { url: '' }.to_json, @headers)
19
+ request(URL::WEBHOOK, url: '')
20
+ end
21
+
22
+ def send(message:, keyboard: {})
23
+ request(
24
+ URL::MESSAGE,
25
+ { receiver: @response.user_id }.merge(message).merge(keyboard)
26
+ )
27
+ end
28
+
29
+ def broadcast(message:, to:)
30
+ request(URL::BROADCAST_MESSAGE, message.merge(broadcast_list: to))
31
+ end
32
+
33
+ def get_account_info
34
+ request(URL::GET_ACCOUNT_INFO)
35
+ end
36
+
37
+ def get_user_details(id:)
38
+ request(URL::GET_USER_DETAILS, id: id)
39
+ end
40
+
41
+ def get_online(ids:)
42
+ request(URL::GET_ONLINE, ids: ids)
43
+ end
44
+
45
+ private
46
+
47
+ def request(url, params = {})
48
+ Faraday.post(url, compact(params).to_json, @headers)
49
+ end
50
+
51
+ def compact(params)
52
+ params.delete_if { |_, v| v.nil? }
18
53
  end
19
54
  end
20
55
  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
@@ -19,6 +19,8 @@ module Viberroo
19
19
  @@os.user_id
20
20
  when 'message'
21
21
  @@os.sender.id
22
+ else
23
+ @@os.dig(:user, :id)
22
24
  end
23
25
  end
24
26
  end
@@ -1,3 +1,3 @@
1
1
  module Viberroo
2
- VERSION = '0.0.9'.freeze
2
+ VERSION = '0.1.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.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor Habchak
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.0.6
72
+ rubygems_version: 3.1.2
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: Viber bot for Ruby.