vk_longpoll_bot 0.0.1 → 0.0.2

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: f28f217393130c2c06d14a05e785bde07781ed164da0032689e15cf02526fae5
4
- data.tar.gz: e3c750777c8347751b940fdc27366cb90b30c2de932894c4456cfd2c05e1f81d
3
+ metadata.gz: ab59094826eca3cea219fd579f9a63d291b0343238e7d88b4b9bb7da846c6e0f
4
+ data.tar.gz: d42e7c1f0a992b8a351a0e9d0c1ca8420f8d606c2bcb07a873b51cae1baaf328
5
5
  SHA512:
6
- metadata.gz: 4ecb287c910584456bbbba055d7af012dba65fffad2657336766c78560f1165c5184d2d45b892232ab228bfc556935d1815def1b9ba3b90a6c17badbff791aa6
7
- data.tar.gz: 143c4c9964f20b9750619820bde70c66c2f8142e48e7396df9443021ef5c90fc10ca648b5c11c7cccf80aa550a7c90d157d9979e224228bc0a14c459278ed330
6
+ metadata.gz: 10bcf660164a69e17770bd8c759dd203ffd151bca8a414345f5d48a332281995302e8d683e763a59c670a68df19b1d46dc5a60ecf9eb44b31baba7d44621ee5b
7
+ data.tar.gz: 477feb6290788606b9ad0703a9e3afede488db7e855643aad1494ef3aae08d7511d0c6520607f916de2deb9d6643a12fdf42cdf62db702a242539ca960e9cd51
data/README.md CHANGED
@@ -1,2 +1,5 @@
1
1
  # vk-longpoll-bot-rb
2
2
  Ruby library to create VK longpoll bot
3
+
4
+ ## Usage
5
+ See examples in *example/*
@@ -3,7 +3,7 @@ module VkLongpollBot
3
3
  # Main class, which contains all the methods of bot.
4
4
  class Bot
5
5
 
6
- # Every bot stores id of group it operates.
6
+ # Every bot stores id of group it operates and hash of event listeners.
7
7
  attr_reader :id, :event_listeners
8
8
 
9
9
  # Initialize bot. This method don't run longpoll.
@@ -26,23 +26,46 @@ module VkLongpollBot
26
26
 
27
27
  # TODO
28
28
  end
29
+
30
+ # =========================================================================
31
+ # SOME API METHODS
32
+ # =========================================================================
29
33
 
30
34
  # Call for api method with given parameters.
31
35
  def api(method_name, parameters = {})
32
36
  Request.api(method_name, parameters, @access_token, @api_version)
33
37
  end
34
38
 
35
- # Messaging
36
-
37
39
  # Send message to <tt>target</tt> with provided <tt>content</tt>.
38
40
  def send_message(target, content)
39
41
  target_id = target.to_i
40
- api("messages.send", user_id: target_id, message: content, random_id: Utility.random_id(target_id))
42
+ random_id = Utility.random_id(target_id)
43
+ api("messages.send", user_id: target_id, message: content, random_id: random_id)
44
+ end
45
+
46
+ # Enable group online status
47
+ def enable_online(gid = @id)
48
+ begin
49
+ api("groups.enableOnline", group_id: gid)
50
+ rescue
51
+ # Online is already enabled
52
+ end
53
+ end
54
+
55
+ # Disable group online status
56
+ def disable_online(gid = @id)
57
+ begin
58
+ api("groups.disableOnline", group_id: gid)
59
+ rescue
60
+ # Online is already disabled
61
+ end
41
62
  end
42
63
 
43
64
  # TODO: Which methods are also addable here?
44
65
 
45
- # Events
66
+ # =========================================================================
67
+ # EVENTS
68
+ # =========================================================================
46
69
 
47
70
  # Add event listener.
48
71
  #
@@ -63,7 +86,9 @@ module VkLongpollBot
63
86
  @on_finish << block
64
87
  end
65
88
 
66
- # Running bot
89
+ # =========================================================================
90
+ # RUNNING AND STOPPING BOT
91
+ # =========================================================================
67
92
 
68
93
  # Start bot. This methods freeze current thread until <tt>stop</tt> called.
69
94
  def run
@@ -80,7 +105,13 @@ module VkLongpollBot
80
105
  @finish_flag = true
81
106
  end
82
107
 
108
+ # =========================================================================
83
109
  private
110
+ # =========================================================================
111
+
112
+ # =========================================================================
113
+ # LONGPOLL
114
+ # =========================================================================
84
115
 
85
116
  # Request longpoll data.
86
117
  def init_longpoll
@@ -119,16 +150,20 @@ module VkLongpollBot
119
150
  end
120
151
  end
121
152
  end
153
+
154
+ # =========================================================================
155
+ # EVENTS
156
+ # =========================================================================
122
157
 
123
158
  # Handle update from longpoll.
124
159
  def update_handler(update)
125
160
  event = Events::Event.new(update["type"], update["object"], update["group_id"], self)
126
161
  @event_listeners[event.subtype].each do |listener|
127
162
  # NOTE: If we had any attributes, we would check whether matching here.
128
- listener.call(event)
163
+ Thread.new(event) { |e| listener.call(e) }
129
164
  end
130
165
  end
131
166
 
132
167
  end
133
168
 
134
- end
169
+ end
@@ -61,16 +61,23 @@ module VkLongpollBot
61
61
  # API error. Must have some code and description.
62
62
  class APIError < ResponseError
63
63
 
64
- attr_reader :code, :error
64
+ attr_reader :error
65
65
 
66
66
  def initialize(error)
67
67
  @error = error["error"]
68
- @code = @error["error_code"]
69
- super(@error["error_msg"])
68
+ super("#{code}: #{included_message}")
69
+ end
70
+
71
+ def code
72
+ @error["error_code"]
73
+ end
74
+
75
+ def included_message
76
+ @error["error_msg"]
70
77
  end
71
78
 
72
79
  def description
73
- CODES[@code]
80
+ CODES[code]
74
81
  end
75
82
 
76
83
  end
@@ -81,4 +88,4 @@ module VkLongpollBot
81
88
 
82
89
  end
83
90
 
84
- end
91
+ end
@@ -14,9 +14,9 @@ module VkLongpollBot
14
14
 
15
15
  # Generate <tt>random_id</tt> for message.
16
16
  def self.random_id(target_id)
17
- (rand(100) * target_id) % 2**32
17
+ (rand(1000) * target_id * Time.now.to_f * 1000).to_i % 2**32
18
18
  end
19
19
 
20
20
  end
21
21
 
22
- end
22
+ end
@@ -2,7 +2,7 @@ Gem::Specification.new do |s|
2
2
  s.name = "vk_longpoll_bot"
3
3
  s.summary = "Provides interface to create simple VK longpoll bot"
4
4
  s.description = "Library to work with VK API and create simple longpoll bot for group."
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.author = "Kuznetsov Vladislav"
7
7
  s.email = "fizvlad@mail.ru"
8
8
  s.homepage = "https://github.com/fizvlad/vk-longpoll-bot-rb"
@@ -13,4 +13,4 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.add_runtime_dependency "rake", "~>12.3"
15
15
  s.add_runtime_dependency "json", "~>2.2"
16
- end
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vk_longpoll_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kuznetsov Vladislav
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-15 00:00:00.000000000 Z
11
+ date: 2019-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake