Class: Lita::Adapters::Vkontakte
- Inherits:
-
Adapter
- Object
- Adapter
- Lita::Adapters::Vkontakte
- Defined in:
- lib/lita/adapters/vkontakte.rb
Overview
VKontakte adapter for the Lita chat bot.
Constant Summary collapse
- API_VERSION =
Used version of VKontakte API. https://vk.com/dev/versions
'5.34'
- REDIRECT_URI =
Needed for VKontakte authentication.
'https:/oauth.vk.com/blank.html'
- HANDLERS =
Handlers for events of VKontakte long poll server. https://vk.com/dev/using_longpoll
{ 4 => :get_message, }
Instance Method Summary collapse
-
#get_message(_message_id, flags, from_id, _timestamp, subject, text, _attachments) ⇒ Object
protected
Handle new message https://vk.com/dev/using_longpoll.
-
#initialize(robot) ⇒ Vkontakte
constructor
Connects to VKontakte API.
-
#run ⇒ Object
The main loop.
-
#send_message(target, message) ⇒ Object
protected
Sends one message to a user or room.
-
#send_messages(target, messages) ⇒ Object
Sends one or more messages to a user or room.
-
#update(a) ⇒ Object
protected
Handle event of VKontakte long poll server.
Constructor Details
#initialize(robot) ⇒ Vkontakte
Connects to VKontakte API.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lita/adapters/vkontakte.rb', line 32 def initialize(robot) super VkontakteApi.configure do |vk| vk.app_id = config.app_id vk.app_secret = config.app_secret vk.redirect_uri = REDIRECT_URI vk.api_version = API_VERSION end @vk = VkontakteApi::Client.new(config.access_token) end |
Instance Method Details
#get_message(_message_id, flags, from_id, _timestamp, subject, text, _attachments) ⇒ Object (protected)
Handle new message https://vk.com/dev/using_longpoll
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/lita/adapters/vkontakte.rb', line 118 def ( # rubocop:disable Metrics/ParameterLists , flags, from_id, , subject, text, ) is_private = subject.start_with?(' ') is_own = flags & 2 != 0 user = User.new(from_id) source = Source.new(user: user, room: subject) = Message.new(robot, text, source) return if is_own .command! if is_private robot.receive() end |
#run ⇒ Object
The main loop. Listens for incoming messages, creates Message objects from them, and dispatches them to the robot.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lita/adapters/vkontakte.rb', line 49 def run # rubocop:disable Metrics/AbcSize, Metrics/MethodLength robot.trigger(:connected) loop do session = @vk..get_long_poll_server url = 'http://' + session.delete(:server) params = session.merge(act: 'a_check', wait: 25, mode: 2) params.ts = @ts if @ts response = nil get_response = lambda do response = VkontakteApi::API.connection.get(url, params).body end while get_response.call break if response.failed? params.ts = @ts = response.ts response.updates.each(&method(:update)) end end ensure robot.trigger(:disconnected) end |
#send_message(target, message) ⇒ Object (protected)
Sends one message to a user or room.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lita/adapters/vkontakte.rb', line 139 def (target, ) # rubocop:disable Metrics/AbcSize is_private = target.room.start_with?(' ') @vk..send({ message: , guid: SecureRandom.random_number(2**31), user_id: (target.user.id.to_i if is_private), chat_id: (target.user.id.to_i - 2_000_000_000 unless is_private), }.reject { |_, v| v.nil? }) end |
#send_messages(target, messages) ⇒ Object
Sends one or more messages to a user or room.
81 82 83 84 85 |
# File 'lib/lita/adapters/vkontakte.rb', line 81 def (target, ) .reject(&:empty?).each do || (target, ) end end |
#update(a) ⇒ Object (protected)
Handle event of VKontakte long poll server. https://vk.com/dev/using_longpoll
100 101 102 103 104 105 |
# File 'lib/lita/adapters/vkontakte.rb', line 100 def update(a) code = a[0] data = a[1..-1] method(HANDLERS[code]).call(*data) if HANDLERS[code] end |