telerb 0.1.5 → 0.1.7

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: 368a13df3fb8d9be794e4c1a1ef14e457b01db5f144c2962a344e93d70bf3921
4
- data.tar.gz: c6117a352b1c0521704ac7ad4abe17149a8018924da52c1197edc2467e80041d
3
+ metadata.gz: 2d98742d8ff3a0d500bc7e3fb45132a54ef4bb174220852e44e218cd6eedf26e
4
+ data.tar.gz: 982e849f2d89e13e4e288c8fb42a98d18d46162d96f034bc88aef48ead277cdd
5
5
  SHA512:
6
- metadata.gz: 6707df98472e8704e0e6b92be7bf82b022ef38a82dc110966b0c651bab0acbb053f9a7754dd413d71c8edcf2bd8d9e6dd95a35599b1d6f9a9597cb28b4b529e6
7
- data.tar.gz: c9b863b42643770bfe5244c9cd3ef2bb8bb97012791ab5d80605cd4556dfe2e181a99ffe67c6479d4f4e8fb81c67a3d7957a7428197b73be5b7b2b856092d9b5
6
+ metadata.gz: 04a96100453a1356741ce4b1cef385b81dcae1c952fe1ed8e54f687adf016bf645786eb1efb52a977be3f11a222c59fd4f58a232bc4ba432827c32987db3a069
7
+ data.tar.gz: d74ceb13616b5ad43da23a6b08c22a5616b9f48b95440534106f5bd27c09c9669a32c20d6f9eb3fb957eef02ae97266bb066fc62085b89957bbb905cfc9f4ed9
data/CHANGELOG.md CHANGED
@@ -27,3 +27,13 @@
27
27
  ## [0.1.5] - 2024-04-10
28
28
 
29
29
  - Implement the method to obtain information from a user
30
+
31
+ ## [0.1.6] - 2024-04-12
32
+
33
+ - Implements The Location sending method
34
+
35
+ ## [0.1.7] - 2024-04-17
36
+
37
+ - Implement Sticker sending method
38
+ - Refactor following best practices
39
+ - Refactor test and implement test execution for parallel execution
data/README.md CHANGED
@@ -21,8 +21,8 @@ If bundler is not being used to manage dependencies, install the gem by executin
21
21
  require 'telerb'
22
22
 
23
23
  # Create a bot instance
24
- bot = TeleRb::TelegramBot.new
25
- bot.config('YOUR_TOKEN')
24
+ config = TelegramConfig.new("YOUR_TOKEN")
25
+ bot = TeleRb::TelegramBot.new(config)
26
26
 
27
27
  # Initialize bot
28
28
  bot.listen do |message|
@@ -46,6 +46,8 @@ The ***message_id*** parameter is only necessary if you want the message to be s
46
46
  bot.send_audio(chat_id, "./exemple.mp3", "test", message_id)
47
47
  # Send Video
48
48
  bot.send_video(chat_id, "./exemple.mp4", "test", message_id)
49
+ # Send Sticker
50
+ bot.send_sticker(chat_id, "./exemple.webp", message_id)
49
51
 
50
52
  ```
51
53
  ### Commands
@@ -82,6 +84,14 @@ bot.user_info(message)
82
84
 
83
85
  ```
84
86
 
87
+ ### Send Location
88
+
89
+ ```
90
+
91
+ bot.location(chat_id, latitude, longitude, message_id)
92
+
93
+ ```
94
+
85
95
  ## Development
86
96
 
87
97
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -2,11 +2,12 @@
2
2
 
3
3
  require "bundler/gem_tasks"
4
4
  require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
5
6
 
6
7
  RSpec::Core::RakeTask.new(:spec)
7
8
 
8
- require "rubocop/rake_task"
9
-
10
9
  RuboCop::RakeTask.new
11
10
 
11
+ require "parallel_tests/tasks"
12
+
12
13
  task default: %i[spec rubocop]
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TeleRb
4
+ # Message Methods
5
+ module ActionsMethods
6
+ def get_commands
7
+ response = CLIENT.get(@config.base_uri("getMyCommands"))
8
+ response.parsed_response["result"]
9
+ rescue StandardError => e
10
+ puts "Error getting list of defined commands: #{e.message}"
11
+ nil
12
+ end
13
+
14
+ def set_commands(commands)
15
+ CLIENT.post(@config.base_uri("setMyCommands"), { commands: commands }.to_json,
16
+ { "Content-Type" => "application/json" })
17
+ "Commands set successfully"
18
+ rescue StandardError => e
19
+ puts "Error defining command list: #{e.message}"
20
+ nil
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TeleRb
4
+ # Message Methods
5
+ module MediaMethods
6
+ def send_photo(chat_id, photo_path, caption = nil, reply_to_message_id = nil)
7
+ send_media(chat_id, photo_path, caption, reply_to_message_id, "sendPhoto", :photo)
8
+ end
9
+
10
+ def send_audio(chat_id, audio_path, caption = nil, reply_to_message_id = nil)
11
+ send_media(chat_id, audio_path, caption, reply_to_message_id, "sendAudio", :audio)
12
+ end
13
+
14
+ def send_video(chat_id, video_path, caption = nil, reply_to_message_id = nil)
15
+ send_media(chat_id, video_path, caption, reply_to_message_id, "sendVideo", :video)
16
+ end
17
+
18
+ def send_document(chat_id, document_path, caption = nil, reply_to_message_id = nil)
19
+ send_media(chat_id, document_path, caption, reply_to_message_id, "sendDocument", :document)
20
+ end
21
+
22
+ def send_sticker(chat_id, sticker_path, reply_to_message_id = nil)
23
+ send_media(chat_id, sticker_path, reply_to_message_id, "sendSticker", :sticker)
24
+ end
25
+
26
+ private
27
+
28
+ def send_media(chat_id, media_path, caption = nil, reply_to_message_id = nil, method, filekey)
29
+ raise ArgumentError, "Invalid media type: {#{filekey.capitalize}" unless %i[photo audio video sticker
30
+ document].include?(filekey)
31
+
32
+ CLIENT.post(@config.base_uri(method),
33
+ {
34
+ chat_id: chat_id,
35
+ reply_to_message_id: reply_to_message_id,
36
+ filekey.to_sym => File.new(media_path, "rb"),
37
+ caption: caption
38
+ },
39
+ { "Content-Type" => "multipart/form-data" })
40
+ "#{filekey.capitalize} Sent with success!"
41
+ rescue StandardError => e
42
+ puts "Error when sending #{filekey.capitalize}: #{e.message}"
43
+ nil
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TeleRb
4
+ # Message Methods
5
+ module MessageMethods
6
+ def send_message(chat_id, text, reply_to_message_id = nil)
7
+ CLIENT.post(@config.base_uri("sendMessage"),
8
+ { chat_id: chat_id, text: text, reply_to_message_id: reply_to_message_id })
9
+ "Message sent successfully!"
10
+ rescue StandardError => e
11
+ puts "Error sending message: #{e.message}"
12
+ nil
13
+ end
14
+
15
+ def forward_message(from_chat_id, to_chat_id, message_id)
16
+ CLIENT.post(@config.base_uri("forwardMessage"),
17
+ { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id })
18
+ "Forwarding was then done successfully!"
19
+ rescue StandardError => e
20
+ puts "Error forwarding message: #{e.message}"
21
+ nil
22
+ end
23
+
24
+ def send_location(chat_id, latitude, longitude, reply_to_message_id = nil)
25
+ CLIENT.post(@config.base_uri("sendLocation"),
26
+ { chat_id: chat_id, latitude: latitude, longitude: longitude,
27
+ reply_to_message_id: reply_to_message_id })
28
+ "Location Sent with success!"
29
+ rescue StandardError => e
30
+ puts "Error when sending Location: #{e.message}"
31
+ nil
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TeleRb
4
+ # Message Methods
5
+ module UserMethods
6
+ def user_info(message)
7
+ message["from"]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "utils/http_client"
4
+ require_relative "methods/message_methods"
5
+ require_relative "methods/media_methods"
6
+ require_relative "methods/actions_methods"
7
+ require_relative "methods/user_methods"
8
+
9
+ module TeleRb
10
+ CLIENT = HttpClient.new
11
+ # class telegram bot
12
+ class TelegramBot
13
+ include MessageMethods
14
+ include MediaMethods
15
+ include ActionsMethods
16
+ include UserMethods
17
+ def initialize(config)
18
+ @config = config
19
+ end
20
+
21
+ def listen(&block)
22
+ loop do
23
+ updates = get_updates(@config.offset)
24
+ updates.each do |update|
25
+ block.call update["message"] if block_given? && update["message"]
26
+ @config.offset = update["update_id"] + 1
27
+ end
28
+ rescue StandardError => e
29
+ puts "Error in listen method: #{e.message}"
30
+ end
31
+ sleep 2
32
+ end
33
+
34
+ private
35
+
36
+ def get_updates(offset = nil)
37
+ response = CLIENT.get(@config.base_uri("getUpdates"), { offset: offset })
38
+ response.parsed_response["result"]
39
+ rescue StandardError => e
40
+ puts "Error in get_updates method: #{e.message}"
41
+ []
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Telegram config
4
+ class TelegramConfig
5
+ attr_accessor :offset
6
+
7
+ def initialize(token)
8
+ @offset = nil
9
+ @token = token
10
+ end
11
+
12
+ def base_uri(method)
13
+ "https://api.telegram.org/bot#{@token}/#{method}"
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TeleRb
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.7"
5
5
  end
data/lib/telerb.rb CHANGED
@@ -1,116 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "telerb/version"
4
- require_relative "telerb/http_client"
5
-
6
- module TeleRb
7
- CLIENT = HttpClient.new
8
- # class telegram bot
9
- class TelegramBot
10
- def initialize
11
- @token = nil
12
- @offset = nil
13
- @base_uri = "https://api.telegram.org/bot"
14
- end
15
-
16
- def config(token)
17
- @token = token
18
- end
19
-
20
- def listen(&block)
21
- loop do
22
- updates = get_updates(@offset)
23
- updates.each do |update|
24
- block.call update["message"] if block_given? && update["message"]
25
- @offset = update["update_id"] + 1
26
- end
27
- rescue StandardError => e
28
- puts "Error in listen method: #{e.message}"
29
- end
30
- sleep 2
31
- end
32
-
33
- def send_message(chat_id, text, reply_to_message_id = nil)
34
- CLIENT.post("#{@base_uri}#{@token}/sendMessage",
35
- { chat_id: chat_id, text: text, reply_to_message_id: reply_to_message_id })
36
- "Message sent successfully!"
37
- rescue StandardError => e
38
- puts "Error sending message: #{e.message}"
39
- nil
40
- end
41
-
42
- def send_photo(chat_id, photo_path, caption = nil, reply_to_message_id = nil)
43
- send_media(chat_id, photo_path, caption, reply_to_message_id, "sendPhoto", :photo)
44
- end
45
-
46
- def send_audio(chat_id, audio_path, caption = nil, reply_to_message_id = nil)
47
- send_media(chat_id, audio_path, caption, reply_to_message_id, "sendAudio", :audio)
48
- end
49
-
50
- def send_video(chat_id, video_path, caption = nil, reply_to_message_id = nil)
51
- send_media(chat_id, video_path, caption, reply_to_message_id, "sendVideo", :video)
52
- end
53
-
54
- def send_document(chat_id, document_path, caption = nil, reply_to_message_id = nil)
55
- send_media(chat_id, document_path, caption, reply_to_message_id, "sendDocument", :document)
56
- end
57
-
58
- def get_commands
59
- response = CLIENT.get("#{@base_uri}#{@token}/getMyCommands")
60
- response.parsed_response["result"]
61
- rescue StandardError => e
62
- puts "Error getting list of defined commands: #{e.message}"
63
- nil
64
- end
65
-
66
- def set_commands(commands)
67
- CLIENT.post("#{@base_uri}#{@token}/setMyCommands", { commands: commands }.to_json,
68
- { "Content-Type" => "application/json" })
69
- "Commands set successfully"
70
- rescue StandardError => e
71
- puts "Error defining command list: #{e.message}"
72
- nil
73
- end
74
-
75
- def forward_message(from_chat_id, to_chat_id, message_id)
76
- CLIENT.post("#{@base_uri}#{@token}/forwardMessage",
77
- { chat_id: to_chat_id, from_chat_id: from_chat_id, message_id: message_id })
78
- "Forwarding was then done successfully!"
79
- rescue StandardError => e
80
- puts "Error forwarding message: #{e.message}"
81
- nil
82
- end
83
-
84
- def user_info(message)
85
- message["from"]
86
- end
87
-
88
- private
89
-
90
- def send_media(chat_id, media_path, caption = nil, reply_to_message_id = nil, method, filekey)
91
- raise ArgumentError, "Invalid media type: {#{filekey.capitalize}" unless %i[photo audio video
92
- document].include?(filekey)
93
-
94
- CLIENT.post("#{@base_uri}#{@token}/#{method}",
95
- {
96
- chat_id: chat_id,
97
- reply_to_message_id: reply_to_message_id,
98
- filekey.to_sym => File.new(media_path, "rb"),
99
- caption: caption
100
- },
101
- { "Content-Type" => "multipart/form-data" })
102
- "#{filekey.capitalize} Sent with success!"
103
- rescue StandardError => e
104
- puts "Error when sending #{filekey.capitalize}: #{e.message}"
105
- nil
106
- end
107
-
108
- def get_updates(offset = nil)
109
- response = CLIENT.get("#{@base_uri}#{@token}/getUpdates", { offset: offset })
110
- response.parsed_response["result"]
111
- rescue StandardError => e
112
- puts "Error in get_updates method: #{e.message}"
113
- []
114
- end
115
- end
116
- end
4
+ require_relative "telerb/telegram_config"
5
+ require_relative "telerb/telegram_bot"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telerb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - CodeByAllan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-11 00:00:00.000000000 Z
11
+ date: 2024-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby library to interact with the Telegram API and create custom bots.
14
14
  email:
@@ -26,7 +26,13 @@ files:
26
26
  - README.md
27
27
  - Rakefile
28
28
  - lib/telerb.rb
29
- - lib/telerb/http_client.rb
29
+ - lib/telerb/methods/actions_methods.rb
30
+ - lib/telerb/methods/media_methods.rb
31
+ - lib/telerb/methods/message_methods.rb
32
+ - lib/telerb/methods/user_methods.rb
33
+ - lib/telerb/telegram_bot.rb
34
+ - lib/telerb/telegram_config.rb
35
+ - lib/telerb/utils/http_client.rb
30
36
  - lib/telerb/version.rb
31
37
  - sig/telerb.rbs
32
38
  homepage: https://github.com/CodeByAllan/TeleRb#readme