vk_longpoll_bot 0.0.1 → 0.0.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 +4 -4
- data/README.md +3 -0
- data/lib/vk_longpoll_bot/bot.rb +43 -8
- data/lib/vk_longpoll_bot/exceptions.rb +12 -5
- data/lib/vk_longpoll_bot/utility.rb +2 -2
- data/vk_longpoll_bot.gemspec +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab59094826eca3cea219fd579f9a63d291b0343238e7d88b4b9bb7da846c6e0f
|
4
|
+
data.tar.gz: d42e7c1f0a992b8a351a0e9d0c1ca8420f8d606c2bcb07a873b51cae1baaf328
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10bcf660164a69e17770bd8c759dd203ffd151bca8a414345f5d48a332281995302e8d683e763a59c670a68df19b1d46dc5a60ecf9eb44b31baba7d44621ee5b
|
7
|
+
data.tar.gz: 477feb6290788606b9ad0703a9e3afede488db7e855643aad1494ef3aae08d7511d0c6520607f916de2deb9d6643a12fdf42cdf62db702a242539ca960e9cd51
|
data/README.md
CHANGED
data/lib/vk_longpoll_bot/bot.rb
CHANGED
@@ -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
|
-
|
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
|
-
#
|
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
|
-
#
|
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(
|
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 :
|
64
|
+
attr_reader :error
|
65
65
|
|
66
66
|
def initialize(error)
|
67
67
|
@error = error["error"]
|
68
|
-
|
69
|
-
|
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[
|
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
|
data/vk_longpoll_bot.gemspec
CHANGED
@@ -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.
|
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.
|
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-
|
11
|
+
date: 2019-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|