vk_cozy 0.3.1 → 0.3.9

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: 94754f3b2073f02952b0eba26be0504fcd4e34c3f822a5c548893110cf6d1e91
4
- data.tar.gz: 4fe38641b5743238015f58bdea8bb61647af1b33a2fe008152da154ff3f73789
3
+ metadata.gz: 3f3e335ff0f9eb232d5a3f259a79592723ee5d5cb785bfb619cd4eb81be11f5a
4
+ data.tar.gz: 6c95df5ab9e7c6bd4ae7dd672786526bbfa4d8aa974ecea4cf564caa6859f627
5
5
  SHA512:
6
- metadata.gz: 173a242c1c7fc7482aba358be78f0c424dabd807da857cfb03036fc2b1f8cc1692ac26f252d232ffded65145fe9b53eebbd377322bc2788fbfd2f864b5793c01
7
- data.tar.gz: bed49f67bfeb6bc25d85d8b7fa5ab137c03b8e4162cf433f12b76414572b9291d42648cf82af31f3329e55ae974613b96e988a6d32979ef061dc7e4b379d120c
6
+ metadata.gz: b7305aa9a7a4da1fb754d4115d6087274e11e7139b56a4028cb557fbc9a98e89b5fc78aad0ab55522094cba6125c18a9865a72728b6509891e3324227b5a9cc3
7
+ data.tar.gz: e3b6b8a3ce259097d50c3fce526474f6564dae1c74c25803903b68759643b12a81669ffc6c5fd5ac6e89bbfc8ebbc1517632743f632434b95b36067ff9562905
data/lib/vk_cozy/bot.rb CHANGED
@@ -2,6 +2,7 @@ require_relative 'types/events/bot_events'
2
2
  require_relative 'dispatch/views/bot/event'
3
3
  require_relative 'framework/labeler/bot'
4
4
  require_relative 'polling/bot_polling'
5
+ require_relative 'dev/keyboard/keyboard'
5
6
 
6
7
  module VkCozy
7
8
  class Bot
@@ -42,7 +43,8 @@ module VkCozy
42
43
  @polling.listen do |event|
43
44
  for event_raw in event['updates']
44
45
  begin
45
- if @labeler.filter(parse_event(event_raw))
46
+ event_raw = parse_event(event_raw)
47
+ if @labeler.filter(event_raw)
46
48
  next
47
49
  end
48
50
  rescue Exception => e
@@ -0,0 +1,72 @@
1
+ module Keyboard
2
+ class BaseAction
3
+ attr_reader :type
4
+
5
+ def get_data
6
+ instance_variables.each_with_object({}) do |k, h|
7
+ var = instance_variable_get("#{k}")
8
+ if not var.nil?
9
+ h[k.to_s.gsub('@', '')] = var
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ class Text < BaseAction
16
+ def initialize(label, payload: nil)
17
+ @type = 'text'
18
+
19
+ @label = label
20
+ @payload = payload
21
+ end
22
+ end
23
+
24
+ class OpenLink < BaseAction
25
+ def initialize(label, link, payload: nil)
26
+ @type = 'open_link'
27
+
28
+ @label = label
29
+ @link = link
30
+ @payload = payload
31
+ end
32
+ end
33
+
34
+ class Location < BaseAction
35
+ def initialize(payload: nil)
36
+ @type = 'location'
37
+
38
+ @payload = payload
39
+ end
40
+ end
41
+
42
+ class VkPay < BaseAction
43
+ def initialize(payload: nil, hash: nil)
44
+ @type = 'vkpay'
45
+
46
+ @payload = payload
47
+ @hash = hash
48
+ end
49
+ end
50
+
51
+ class VkApps < BaseAction
52
+ def initialize(app_id, owner_id, payload: nil, label: nil, hash: nil)
53
+ @type = 'open_app'
54
+
55
+ @app_id = app_id
56
+ @owner_id = owner_id
57
+ @payload = payload
58
+ @label = label
59
+ @hash = hash
60
+ end
61
+ end
62
+
63
+
64
+ class Callback < BaseAction
65
+ def initialize(label, payload)
66
+ @type = 'callback'
67
+
68
+ @label = label
69
+ @payload = payload
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,34 @@
1
+ module Keyboard
2
+ class KeyboardButton
3
+ def initialize(action, color: nil, data: nil)
4
+ @action = action
5
+ @color = color
6
+ @data = data
7
+ end
8
+
9
+ def self.from_typed(action, color: nil)
10
+ return self.new(action, color: color, data: nil)
11
+ end
12
+
13
+ def self.from_hash(data)
14
+ color = data.key('color')
15
+ keyboard_data = {'action' => data}
16
+ if color.nil?
17
+ keyboard_data['action'].delete('color')
18
+ keyboard_data['color'] = color
19
+ end
20
+ return self.new(self.action, self.color, keyboard_data)
21
+ end
22
+
23
+ def get_data
24
+ if not @data.nil?
25
+ return @data
26
+ end
27
+ data = {'action' => @action.get_data()}
28
+ if ['text', 'callback'].include?(@action.type) and not @color.nil?
29
+ data['color'] = @color
30
+ end
31
+ return data
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ module Keyboard
2
+ class Color
3
+ include Ruby::Enum
4
+
5
+ define :PRIMARY, 'primary'
6
+ define :SECONDARY, 'secondary'
7
+ define :NEGATIVE, 'negative'
8
+ define :POSITIVE, 'positive'
9
+ end
10
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'action'
2
+ require_relative 'button'
3
+ require_relative 'color'
4
+
5
+ module Keyboard
6
+ class Keyboard
7
+ def initialize(one_time: false, inline: false)
8
+ @one_time = one_time
9
+ @inline = inline
10
+ @buttons = []
11
+ end
12
+
13
+ def row()
14
+ @buttons << []
15
+ return self
16
+ end
17
+
18
+ def add(action, color=nil)
19
+ if @buttons.length == 0
20
+ row()
21
+ end
22
+ button = KeyboardButton.from_typed(action, color: color)
23
+ @buttons[-1] << button
24
+ return self
25
+ end
26
+
27
+ def get_json()
28
+ _buttons = []
29
+ for row in @buttons
30
+ buttons = []
31
+ for button in row
32
+ buttons << button.get_data()
33
+ end
34
+ _buttons << buttons
35
+ end
36
+ data = {
37
+ 'one_time' => @one_time,
38
+ 'inline' => @inline,
39
+ 'buttons' => _buttons
40
+ }
41
+ return data
42
+ end
43
+
44
+ def to_s
45
+ return JSON.generate(get_json())
46
+ end
47
+ end
48
+ end
@@ -19,6 +19,14 @@ module VkCozy
19
19
 
20
20
  end
21
21
 
22
+ def answer(text)
23
+ return @api.messages_send(
24
+ peer_id: @obj.peer_id,
25
+ message: text,
26
+ random_id: 0
27
+ )
28
+ end
29
+
22
30
  def [] key
23
31
  instance_variable_get("@#{key}")
24
32
  end
@@ -1,39 +1,78 @@
1
1
  module VkCozy
2
+
3
+ class Bothandler
4
+ attr_reader :stop
5
+
6
+ def initialize(filter, func, stop: true)
7
+ @stop = stop
8
+ @filter = filter
9
+ @func = func
10
+ end
11
+
12
+ def check(event)
13
+ check_bot = @filter.check_bot(event)
14
+ if check_bot
15
+ if check_bot.is_a?(Symbol)
16
+ return true
17
+ elsif check_bot.is_a?(Hash)
18
+ @func.call(event, check_bot)
19
+ else
20
+ @func.call(event)
21
+ end
22
+ if @stop
23
+ return true
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+
2
30
  class BotLabeler
3
31
  attr_reader :api
4
32
 
5
33
  def initialize(api)
6
34
  @api = api
7
- @rules = []
35
+ @handlers = {'*' => []} # * - обрабатывает события в не зависимости от их типа.
36
+ end
37
+
38
+ def get_handlers(type)
39
+ if @handlers[type].nil?
40
+ return []
41
+ else
42
+ return @handlers[type]
43
+ end
8
44
  end
9
45
 
10
46
  def filter(event)
11
- for i in @rules
12
- f = i[:filter]
13
- check = f.check_bot(event)
14
- if check
15
- if check.is_a?(Symbol)
16
- check = [Symbol]
17
- elsif check.is_a?(Array)
18
- i[:func].call(event, *check)
19
- elsif check.is_a?(Hash)
20
- i[:func].call(event, **check)
21
- else
22
- i[:func].call(event)
23
- end
47
+ for handler in get_handlers(event.type) + get_handlers('*')
48
+ if handler.check(event)
24
49
  return true
25
50
  end
26
51
  end
27
52
  end
28
53
 
29
- def message_handler(filter, func)
54
+ def add_handler(filter, func, type: '*')
30
55
  if func.is_a?(Symbol)
31
56
  func = method(func)
32
57
  end
33
- @rules << {
34
- :func => func,
35
- :filter => filter
36
- }
58
+
59
+ if @handlers[type].nil?
60
+ @handlers[type] = []
61
+ end
62
+
63
+ @handlers[type] << Bothandler.new(filter, func)
64
+ end
65
+
66
+ def event_handler(filter, func)
67
+ add_handler(filter, func, type: 'message_event')
68
+ end
69
+
70
+ def message_handler(filter, func)
71
+ add_handler(filter, func, type: 'message_new')
72
+ end
73
+
74
+ def handler(filter, func)
75
+ add_handler(filter, func)
37
76
  end
38
77
  end
39
78
  end
@@ -1,4 +1,4 @@
1
- module VkCozy
1
+ module Filter
2
2
  class BaseFilter
3
3
  def check_user(event) # Method check for user-bot
4
4
  raise 'Method check_user not implemented'
@@ -9,7 +9,17 @@ module VkCozy
9
9
  end
10
10
  end
11
11
 
12
- class YaScan < VkCozy::BaseFilter
12
+ class MockedFilter < BaseFilter
13
+ def check_bot(event)
14
+ return true
15
+ end
16
+
17
+ def user_bot(event)
18
+ return true
19
+ end
20
+ end
21
+
22
+ class YaScan < BaseFilter
13
23
  def initialize(pattern, flags: Regexp::IGNORECASE)
14
24
  @flags = flags
15
25
  @pattern = valid_pattern(pattern) # Pattern example: my name is <name>
@@ -32,25 +42,20 @@ module VkCozy
32
42
  end
33
43
 
34
44
  def check_bot(event)
35
- if event.type == VkCozy::BotEventType::MESSAGE_NEW
36
- check_text(event.message.text)
37
- end
45
+ check_text(event.message.text)
38
46
  end
39
47
 
40
48
  def check_user(event)
41
- if event.type == VkCozy::UserEventType::MESSAGE_NEW
42
- if event.from_me
43
- return false
44
- end
45
- check_text(event.text)
49
+ if event.from_me
50
+ return false
46
51
  end
52
+ check_text(event.text)
47
53
  end
48
54
  end
49
55
 
50
56
  class Text < BaseFilter
51
- def initialize(regex, **kwargs)
57
+ def initialize(regex)
52
58
  @regex = regex
53
- @raw = kwargs
54
59
  end
55
60
 
56
61
  def check_user(event)
@@ -1,40 +1,74 @@
1
1
  module VkCozy
2
+
3
+ class UserHandler
4
+ def initialize(filter, func, stop: true)
5
+ @filter = filter
6
+ @func = func
7
+ @stop = stop
8
+ end
9
+
10
+ def check(event)
11
+ check_user = @filter.check_user(event)
12
+ if check_user
13
+ if check_user.is_a?(Symbol)
14
+ return true
15
+ elsif check_user.is_a?(Hash)
16
+ @func.call(event, check_user)
17
+ else
18
+ @func.call(event)
19
+ end
20
+
21
+ if @stop
22
+ return true
23
+ end
24
+ end
25
+ end
26
+ end
27
+
2
28
  class UserLabeler
3
29
  attr_reader :api
4
30
 
5
31
  def initialize(api)
6
32
  @api = api
7
- @rules = []
33
+ @handlers = {'*' => []}
34
+ end
35
+
36
+ def get_handlers(type)
37
+ if @handlers[type].nil?
38
+ return []
39
+ else
40
+ return @handlers[type]
41
+ end
8
42
  end
9
43
 
10
44
  def filter(event_raw)
11
45
  event = VkCozy::UserEvent.new(@api, event_raw)
12
- for i in @rules
13
- f = i[:filter]
14
- check = f.check_user(event)
15
- if check
16
- if check.is_a?(Symbol)
17
- check = [Symbol]
18
- elsif check.is_a?(Array)
19
- i[:func].call(event, *check)
20
- elsif check.is_a?(Hash)
21
- i[:func].call(event, **check)
22
- else
23
- i[:func].call(event)
24
- end
46
+ puts event
47
+ for handler in get_handlers(event.type) + get_handlers('*')
48
+ if handler.check(event)
25
49
  return true
26
50
  end
27
51
  end
28
52
  end
29
53
 
30
- def message_handler(filter, func)
54
+ def add_handler(filter, func, type: '*')
31
55
  if func.is_a?(Symbol)
32
56
  func = method(func)
33
57
  end
34
- @rules << {
35
- :func => func,
36
- :filter => filter
37
- }
58
+
59
+ if @handlers[type].nil?
60
+ @handlers[type] = []
61
+ end
62
+
63
+ @handlers[type] << UserHandler.new(filter, func)
64
+ end
65
+
66
+ def message_handler(filter, func)
67
+ add_handler(filter, func, type: VkCozy::UserEventType::MESSAGE_NEW)
68
+ end
69
+
70
+ def handler(filter, func)
71
+ add_handler(filter, func)
38
72
  end
39
73
  end
40
74
  end
@@ -25,7 +25,7 @@ module VkCozy
25
25
 
26
26
  define :VIDEO_NEW, 'video_new'
27
27
 
28
- define :VIDEO_COMMENT_NEW, 'video_comment_new'
28
+ define :VIDEO_COMMENT_NEW, 'video_comment_new'
29
29
  define :VIDEO_COMMENT_EDIT, 'video_comment_edit'
30
30
  define :VIDEO_COMMENT_RESTORE, 'video_comment_restore'
31
31
 
@@ -53,12 +53,22 @@ module VkCozy
53
53
  VkCozy::UserEventType::RESET_MESSAGE_FLAGS => [MSGID, 'mask'] + MESSAGE_EXTRA_FIELDS,
54
54
  VkCozy::UserEventType::MESSAGE_NEW => [MSGID, 'flags'] + MESSAGE_EXTRA_FIELDS,
55
55
  VkCozy::UserEventType::MESSAGE_EDIT => [MSGID, 'mask'] + MESSAGE_EXTRA_FIELDS,
56
+
56
57
  VkCozy::UserEventType::IN_READ => ['peer_id', 'local_id'],
57
58
  VkCozy::UserEventType::OUT_READ => ['peer_id', 'local_id'],
59
+
58
60
  VkCozy::UserEventType::FRIEND_ONLINE => ['user_id', 'extra', 'timestamp'],
59
61
  VkCozy::UserEventType::FRIEND_OFFLINE => ['user_id', 'flags', 'timestamp'],
60
- VkCozy::UserEventType::DIALOG_TYPING_STATE => ['user_id', 'flags']
62
+
63
+ VkCozy::UserEventType::CHAT_EDIT => ['chat_id', 'self'],
64
+ VkCozy::UserEventType::CHAT_INFO_EDIT => ['type_id', 'peer_id', 'info'],
65
+
66
+ VkCozy::UserEventType::DIALOG_TYPING_STATE => ['user_id', 'flags'],
67
+ VkCozy::UserEventType::CHAT_TYPING_STATE => ['user_id', 'chat_id'],
68
+
69
+ VkCozy::UserEventType::COUNTER => ['count']
61
70
  }
71
+
62
72
  PARSE_PEER_ID_EVENTS = EVENT_ATTRS_MAPPING.map{|k, v| if v.include?('peer_id') then k end}.select{ |i| not i.nil? }
63
73
  PARSE_MESSAGE_FLAGS_EVENTS = [
64
74
  VkCozy::UserEventType::REPLACE_MESSAGE_FLAGS,
data/lib/vk_cozy.rb CHANGED
@@ -5,4 +5,4 @@ require 'hash_dot'
5
5
  require_relative 'vk_cozy/api/api'
6
6
  require_relative 'vk_cozy/bot'
7
7
  require_relative 'vk_cozy/user'
8
- require_relative 'vk_cozy\framework\labeler\filters\filters'
8
+ require_relative 'vk_cozy/framework/labeler/filters/filters'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vk_cozy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danil Konenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-11 00:00:00.000000000 Z
11
+ date: 2022-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inum
@@ -52,7 +52,8 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: framework for Vk.api
55
+ description: vk_cozy является фреймворком для vk api, имеет удобные хэндлеры и фильтры,
56
+ создан чтобы упростить разработку ботов для вк
56
57
  email: googloldanil@gmail.com
57
58
  executables: []
58
59
  extensions: []
@@ -62,6 +63,10 @@ files:
62
63
  - lib/vk_cozy.rb
63
64
  - lib/vk_cozy/api/api.rb
64
65
  - lib/vk_cozy/bot.rb
66
+ - lib/vk_cozy/dev/keyboard/action.rb
67
+ - lib/vk_cozy/dev/keyboard/button.rb
68
+ - lib/vk_cozy/dev/keyboard/color.rb
69
+ - lib/vk_cozy/dev/keyboard/keyboard.rb
65
70
  - lib/vk_cozy/dispatch/views/bot/event.rb
66
71
  - lib/vk_cozy/dispatch/views/user/event.rb
67
72
  - lib/vk_cozy/framework/labeler/bot.rb
@@ -84,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
89
  requirements:
85
90
  - - ">="
86
91
  - !ruby/object:Gem::Version
87
- version: '0'
92
+ version: 2.7.0
88
93
  required_rubygems_version: !ruby/object:Gem::Requirement
89
94
  requirements:
90
95
  - - ">="
@@ -94,5 +99,5 @@ requirements: []
94
99
  rubygems_version: 3.3.7
95
100
  signing_key:
96
101
  specification_version: 4
97
- summary: Vk.com API Client
102
+ summary: vk_cozy framework for vk-api
98
103
  test_files: []