icqbot 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c6c40f720f2eab21955a65f3a3f034d7d260f8393db505cf6ef72dc0089dd835
4
+ data.tar.gz: c9c5930e02ef29769fd6aa92f1727d5b748d2b95ab97ca8e0ab61cc177c217cc
5
+ SHA512:
6
+ metadata.gz: 046770ad26085c34667c434cb7af1ac0e3f77c1dd3449c9bd0a30121ee29dea25c4888cb03b7b0d9ad4a54c1d366d9fba7190411a68af6b4d2a2f00a869fbd91
7
+ data.tar.gz: 8ce652b39117e17729be909426dcc95f54fc99424e406483fe5409f587502a4368657190f136c07ffd50216b27fab8c9be0c89f1847362c211072a9fb5bc45aa
@@ -0,0 +1,3 @@
1
+ .bundle
2
+ test
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'requests', '~> 1.0', '>= 1.0.2'
@@ -0,0 +1,27 @@
1
+
2
+ icq-bot-ruby in working :zzz:
3
+ ------------
4
+ Ruby wrapper for [ICQ Bot API](https://icq.com/botapi/)
5
+
6
+ ----
7
+
8
+ ## Installation
9
+ ```
10
+ gem install icqbot
11
+ ```
12
+
13
+ ## Usage
14
+ Simple echo bot:
15
+ ```ruby
16
+ require 'icqbot'
17
+
18
+ ICQ::Bot.new('token') do |bot|
19
+ bot.listen do |event|
20
+ bot.send_msg("echo #{event.text}", event.chat_id)
21
+ end
22
+ end
23
+ ```
24
+
25
+ ### Credits
26
+ * Thank you Cyril David (author by gem requests)
27
+ * And thank you Matz for wonderful Ruby
@@ -0,0 +1,10 @@
1
+ require_relative '../lib/icqbot.rb'
2
+
3
+ times, max_time = 0, 5
4
+
5
+ ICQ::Bot.new('token') do |bot|
6
+ bot.listen do |event|
7
+ bot.send_msg("echo '#{event.text}'", event.chat_id)
8
+ bot.loop = false if (times += 1) != max_time
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../lib/icqbot.rb'
2
+
3
+ ICQ::Bot.new('token') do |bot|
4
+ bot.listen do |event|
5
+ bot.send_msg "echo #{event.text}", event.chat_id
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../lib/icqbot.rb'
2
+
3
+ ICQ::Bot.new('token') do |bot|
4
+ bot.listen do |event|
5
+ msg = ICQ::Message.new(
6
+ "echo #{event.text}", ICQ::Button.new('google', url: 'google.com'))
7
+ bot.send_msg msg, event.chat_id
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "icqbot"
6
+ s.version = "0.1.0"
7
+ s.authors = ["sheyber"]
8
+ s.email = ["ukt1@ro.ru"]
9
+ # s.license = 'MIT'
10
+
11
+ s.summary = %q{Ruby wrapper for ICQ Bot API.}
12
+ s.description = %q{This gem is a simple and minimalistic library for creating ICQ bots.}
13
+
14
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ s.bindir = 'exe'
16
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_dependency 'requests'
20
+ end
@@ -0,0 +1,9 @@
1
+ require "requests/sugar"
2
+
3
+ require_relative './icqbot/bot.rb'
4
+ require_relative './icqbot/event.rb'
5
+ require_relative './icqbot/user.rb'
6
+ require_relative './icqbot/message.rb'
7
+
8
+ module ICQ
9
+ end
@@ -0,0 +1,62 @@
1
+ require 'json'
2
+
3
+ require_relative '../icqbot.rb'
4
+ require_relative './urls_api.rb'
5
+
6
+ require_relative './functional/send_msg.rb'
7
+ require_relative './functional/edit_msg.rb'
8
+ require_relative './functional/delete_msg.rb'
9
+ require_relative './functional/chats/get_info.rb'
10
+
11
+ module ICQ
12
+
13
+ class Bot
14
+ attr_accessor :loop
15
+
16
+ def initialize token, pool_time=30
17
+ @token = token
18
+ @pool_time = pool_time
19
+ @last_event_id = 0
20
+ @loop = true
21
+ yield self if block_given?
22
+ end
23
+
24
+ def get_events # /events/get
25
+ params = {
26
+ 'token': @token,
27
+ 'lastEventId': @last_event_id,
28
+ 'pollTime': @pool_time
29
+ }
30
+ Requests.get(URLS_API::GET_EVENTS, params: params)
31
+ end
32
+
33
+ def listen # event loop
34
+ while @loop
35
+ events = JSON::load(get_events.body)
36
+ if events and events['events'] and events['events'] != []
37
+ last_event = events['events'].last
38
+ @last_event_id = last_event['eventId']
39
+ yield ICQ::Event.new last_event
40
+ end
41
+ end
42
+ end
43
+
44
+ def last_event # FIXME: its can delete
45
+ events = JSON::load(get_events.body)
46
+ if events and events['events'] and events['events'] != []
47
+ last_event = events['events'].last
48
+ @last_event_id = last_event['eventId']
49
+ yield ICQ::Event.new last_event
50
+ end
51
+ end
52
+
53
+ private
54
+ def base_req chat_id
55
+ {
56
+ 'token': @token,
57
+ 'chatId': chat_id
58
+ }
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../icqbot.rb'
2
+
3
+ module ICQ
4
+
5
+ module TypeEvent
6
+ NEW_MSG = 'newMessage'
7
+ EDITED_MSG = 'editedMessage'
8
+ DELETED_MSG = 'deletedMessage'
9
+ end
10
+
11
+ class Event
12
+ attr_reader :type, :text, :msg_id, :chat_id, :from
13
+
14
+ def initialize event_h
15
+ @event_h = event_h
16
+ @type = event_h['type']
17
+ @text = event_h['payload']['text']
18
+ @msg_id = event_h['payload']['msgId']
19
+ @chat_id = event_h['payload']['chat']['chatId']
20
+ @from = event_h['payload']['from']
21
+ end
22
+
23
+ def to_h; @event_h end
24
+ end
25
+
26
+ end
@@ -0,0 +1,14 @@
1
+ require_relative '../../bot.rb'
2
+
3
+ module ICQ
4
+
5
+ class Bot
6
+ def get_info chat_id
7
+ # TODO: make a class for chats, kind of
8
+ params = base_req chat_id
9
+ json = JSON::load Requests.get(URLS_API::GET_INFO, params: params).body
10
+ return ICQ::User.new json if json['firstName']
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require_relative '../bot.rb'
2
+
3
+ module ICQ
4
+
5
+ class Bot
6
+ def delete_msg msg_id, chat_id
7
+ # FIXME: fix this trash
8
+ params = base_req chat_id
9
+ r = "https://api.icq.net/bot/v1/messages/deleteMessages?token=#{params[:token]}&chatId=#{chat_id}&msgId=#{msg_id}" # HACK: super trash
10
+ JSON::load Requests.get(r).body
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,13 @@
1
+ require_relative '../bot.rb'
2
+
3
+ module ICQ
4
+
5
+ class Bot
6
+ def edit_msg msg, msg_id, chat_id
7
+ params = create_message_params msg, chat_id
8
+ params['msgId'] = msg_id
9
+ JSON::load Requests.get(URLS_API::EDIT_MSG, params: params).body
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../bot.rb'
2
+
3
+ module ICQ
4
+
5
+ class Bot
6
+ def send_msg msg, chat_id
7
+ params = create_message_params msg, chat_id
8
+ JSON::load Requests.get(URLS_API::SEND_MSG, params: params).body
9
+ end
10
+
11
+ private
12
+ def create_message_params msg, chat_id
13
+ params = base_req chat_id
14
+ if msg.is_a? ICQ::Message
15
+ params['text'] = msg.text
16
+ if msg.keyboard and msg.keyboard != []
17
+ kb = msg.keyboard.map(&:to_h).map(&:to_json)
18
+ params['inlineKeyboardMarkup'] = "[[#{kb.join(',')}]]"
19
+ end
20
+ elsif msg.is_a? String
21
+ params['text'] = msg
22
+ else
23
+ raise ArgumentError.new "message can't be a #{msg.class}"
24
+ end
25
+ return params
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,29 @@
1
+ require_relative '../icqbot.rb'
2
+
3
+ module ICQ
4
+
5
+ class Message
6
+ attr_reader :text, :keyboard
7
+
8
+ def initialize obj, *keyboard
9
+ @text = obj
10
+ @keyboard = keyboard
11
+ end
12
+ end
13
+
14
+ class Button
15
+ def initialize text, style:'attention', url:nil, call_back_data:nil
16
+ @button = {
17
+ 'text': text,
18
+ 'style': style,
19
+ }
20
+ @button['url'] = url if url
21
+ @button['callbackData'] = call_back_data if call_back_data
22
+ end
23
+
24
+ def to_h
25
+ @button
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module URLS_API
3
+ GET_EVENTS = 'https://api.icq.net/bot/v1/events/get'
4
+ SEND_MSG = 'https://api.icq.net/bot/v1/messages/sendText'
5
+ EDIT_MSG = 'https://api.icq.net/bot/v1/messages/editText'
6
+ DEL_MSG = 'https://api.icq.net/bot/v1/messages/deleteMessages'
7
+ GET_INFO = 'https://api.icq.net/bot/v1/chats/getInfo'
8
+ end
@@ -0,0 +1,16 @@
1
+ require_relative '../icqbot.rb'
2
+
3
+ module ICQ
4
+
5
+ class User
6
+ attr_reader :first_name, :last_name, :photo, :type
7
+
8
+ def initialize user_h
9
+ @first_name = user_h['firstName']
10
+ @last_name = user_h['lastName']
11
+ @photo = user_h['photo'].first['url']
12
+ @type = user_h['type']
13
+ end
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icqbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sheyber
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: requests
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This gem is a simple and minimalistic library for creating ICQ bots.
28
+ email:
29
+ - ukt1@ro.ru
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - bin/5time_echo_and_end.rb
38
+ - bin/echo_bot.rb
39
+ - bin/send_text_with_button.rb
40
+ - icqbot.gemspec
41
+ - lib/icqbot.rb
42
+ - lib/icqbot/bot.rb
43
+ - lib/icqbot/event.rb
44
+ - lib/icqbot/functional/chats/get_info.rb
45
+ - lib/icqbot/functional/delete_msg.rb
46
+ - lib/icqbot/functional/edit_msg.rb
47
+ - lib/icqbot/functional/send_msg.rb
48
+ - lib/icqbot/message.rb
49
+ - lib/icqbot/urls_api.rb
50
+ - lib/icqbot/user.rb
51
+ homepage:
52
+ licenses: []
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.4
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Ruby wrapper for ICQ Bot API.
73
+ test_files: []