Class: VKTeams::Bot
- Inherits:
-
Object
- Object
- VKTeams::Bot
- Defined in:
- lib/vkteamsbot/bot.rb,
lib/vkteamsbot/functional/edit_msg.rb,
lib/vkteamsbot/functional/send_msg.rb,
lib/vkteamsbot/functional/delete_msg.rb,
lib/vkteamsbot/functional/chats/get_info.rb,
lib/vkteamsbot/functional/chats/get_admins.rb,
lib/vkteamsbot/functional/chats/get_members.rb,
lib/vkteamsbot/functional/chats/get_blocked_users.rb,
lib/vkteamsbot/functional/chats/administration/set_about.rb,
lib/vkteamsbot/functional/chats/administration/set_rules.rb,
lib/vkteamsbot/functional/chats/administration/set_title.rb,
lib/vkteamsbot/functional/chats/administration/pin_unpin_msg.rb,
lib/vkteamsbot/functional/chats/administration/block_unblock_user.rb
Instance Attribute Summary collapse
-
#loop ⇒ Object
Returns the value of attribute loop.
Instance Method Summary collapse
- #add_callback_handler(data, handler) ⇒ Object
- #add_handler(text, handler) ⇒ Object
- #block_user(user_id, chat_id, del_last_msg = false) ⇒ Object
- #delete_msg(msg_id, chat_id) ⇒ Object
- #edit_msg(msg, msg_id, chat_id) ⇒ Object
- #get_admins(chat_id) ⇒ Object
- #get_blocked_users(chat_id) ⇒ Object
-
#get_events ⇒ Object
/events/get.
- #get_info(chat_id) ⇒ Object
- #get_members(chat_id, cursor = nil) ⇒ Object
-
#initialize(token, pool_time = 30, verbose = false) {|_self| ... } ⇒ Bot
constructor
A new instance of Bot.
-
#listen ⇒ Object
event loop.
- #pin_msg(msg_id, chat_id) ⇒ Object
- #send_msg(msg, chat_id, parseMode = nil) ⇒ Object
- #set_about(chat_id, about) ⇒ Object
- #set_rules(chat_id, rules) ⇒ Object
- #set_title(chat_id, title) ⇒ Object
- #title=(o) ⇒ Object
- #unblock_user(user_id, chat_id) ⇒ Object
- #unpin_msg(msg_id, chat_id) ⇒ Object
Constructor Details
#initialize(token, pool_time = 30, verbose = false) {|_self| ... } ⇒ Bot
Returns a new instance of Bot.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/vkteamsbot/bot.rb', line 23 def initialize token, pool_time=30, verbose=false @token = token @pool_time = pool_time @last_event_id = 0 @loop = true @handlers = {} @callback_handlers = {} @verbose = verbose yield self if block_given? end |
Instance Attribute Details
#loop ⇒ Object
Returns the value of attribute loop.
21 22 23 |
# File 'lib/vkteamsbot/bot.rb', line 21 def loop @loop end |
Instance Method Details
#add_callback_handler(data, handler) ⇒ Object
65 66 67 |
# File 'lib/vkteamsbot/bot.rb', line 65 def add_callback_handler data, handler @callback_handlers[data] = handler end |
#add_handler(text, handler) ⇒ Object
61 62 63 |
# File 'lib/vkteamsbot/bot.rb', line 61 def add_handler text, handler @handlers[text] = handler end |
#block_user(user_id, chat_id, del_last_msg = false) ⇒ Object
6 7 8 9 10 |
# File 'lib/vkteamsbot/functional/chats/administration/block_unblock_user.rb', line 6 def block_user user_id, chat_id, del_last_msg=false params = base_req_for_block_unblock_user user_id, chat_id, del_last_msg json_load Requests.get( API.block_user, params: params).body end |
#delete_msg(msg_id, chat_id) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/delete_msg.rb', line 6 def delete_msg msg_id, chat_id # FIXME: fix this trash params = base_req chat_id r = API. + "?token=#{params[:token]}&chatId=#{chat_id}&msgId=#{msg_id}" # HACK: super trash json_load Requests.get(r).body end |
#edit_msg(msg, msg_id, chat_id) ⇒ Object
6 7 8 9 10 |
# File 'lib/vkteamsbot/functional/edit_msg.rb', line 6 def edit_msg msg, msg_id, chat_id params = msg, chat_id params['msgId'] = msg_id json_load Requests.get(API.edit_text, params: params).body end |
#get_admins(chat_id) ⇒ Object
6 7 8 9 10 |
# File 'lib/vkteamsbot/functional/chats/get_admins.rb', line 6 def get_admins chat_id _ = json_load Requests.get( API.get_admins, params: base_req(chat_id)).body _['admins'] end |
#get_blocked_users(chat_id) ⇒ Object
6 7 8 9 10 |
# File 'lib/vkteamsbot/functional/chats/get_blocked_users.rb', line 6 def get_blocked_users chat_id _ = json_load Requests.get( API.get_blocked_users, params: base_req(chat_id)).body _['users'] end |
#get_events ⇒ Object
/events/get
34 35 36 37 38 39 40 41 |
# File 'lib/vkteamsbot/bot.rb', line 34 def get_events # /events/get params = { 'token': @token, 'lastEventId': @last_event_id, 'pollTime': @pool_time } Requests.get(API.get_events, params: params) end |
#get_info(chat_id) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/chats/get_info.rb', line 6 def get_info chat_id # TODO: make a class for chats, kind of params = base_req chat_id json = json_load Requests.get(API.get_info, params: params).body return VKTeams::User.new json if json['firstName'] end |
#get_members(chat_id, cursor = nil) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/chats/get_members.rb', line 6 def get_members chat_id, cursor=nil # TODO: сделать что-то с cursor _ = json_load Requests.get( API.get_members, params: base_req(chat_id)).body _['members'] end |
#listen ⇒ Object
event loop
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vkteamsbot/bot.rb', line 43 def listen # event loop while @loop events = json_load(get_events.body) if events and events['events'] and events['events'] != [] last_event = events['events'].last @last_event_id = last_event['eventId'] last_event = VKTeams::Event.new last_event if @handlers.has_key? last_event.text @handlers[last_event.text].call last_event elsif last_event.type == VKTeams::TypeEvent::CALLBACK and @callback_handlers.has_key? last_event.data @callback_handlers[last_event.text].call last_event else yield last_event end end end end |
#pin_msg(msg_id, chat_id) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/chats/administration/pin_unpin_msg.rb', line 6 def pin_msg msg_id, chat_id params = base_req chat_id params['msgId'] = msg_id json_load Requests.get( API., params: params).body end |
#send_msg(msg, chat_id, parseMode = nil) ⇒ Object
6 7 8 9 |
# File 'lib/vkteamsbot/functional/send_msg.rb', line 6 def send_msg msg, chat_id, parseMode = nil params = msg, chat_id, parseMode json_load Requests.get(API.send_text, params: params).body end |
#set_about(chat_id, about) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/chats/administration/set_about.rb', line 6 def set_about chat_id, about params = base_req chat_id params['about'] = about json_load Requests.get( API.set_about, params: params).body end |
#set_rules(chat_id, rules) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/chats/administration/set_rules.rb', line 6 def set_rules chat_id, rules params = base_req chat_id params['rules'] = rules json_load Requests.get( API.set_rules, params: params).body end |
#set_title(chat_id, title) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/vkteamsbot/functional/chats/administration/set_title.rb', line 6 def set_title chat_id, title params = base_req chat_id params['title'] = title json_load Requests.get( API.set_title, params: params).body end |
#title=(o) ⇒ Object
13 14 15 |
# File 'lib/vkteamsbot/functional/chats/administration/set_title.rb', line 13 def title= o set_title *o end |
#unblock_user(user_id, chat_id) ⇒ Object
12 13 14 15 16 |
# File 'lib/vkteamsbot/functional/chats/administration/block_unblock_user.rb', line 12 def unblock_user user_id, chat_id params = base_req_for_block_unblock_user user_id, chat_id json_load Requests.get( API.unblock_user, params: params).body end |
#unpin_msg(msg_id, chat_id) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/vkteamsbot/functional/chats/administration/pin_unpin_msg.rb', line 13 def unpin_msg msg_id, chat_id params = base_req chat_id params['msgId'] = msg_id json_load Requests.get( API., params: params).body end |