From ef39aabac7808e3a85461ce79a6794db9539bf36 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Sun, 28 Oct 2018 09:08:48 -0500 Subject: [PATCH 01/20] add RVM project files to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 9f74d68..aced27e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,7 @@ mkmf.log *.iml *.ipr *.iws +.ruby-version +.ruby-gemset +.rvmrc +.versions.conf \ No newline at end of file From 422db73466d43413b567772f715a5e9c3574dcec Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Sun, 28 Oct 2018 09:15:27 -0500 Subject: [PATCH 02/20] add is_bot and language_code attributes to TelegramBot::User --- lib/telegram_bot/user.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/telegram_bot/user.rb b/lib/telegram_bot/user.rb index 77ba935..6adab27 100644 --- a/lib/telegram_bot/user.rb +++ b/lib/telegram_bot/user.rb @@ -6,6 +6,8 @@ class User attribute :first_name, String attribute :last_name, String attribute :username, String + attribute :is_bot, Boolean, default_value: false + attribute :language_code, String def ==(other) other.is_a?(self.class) && other.hash == hash From 6b9073e2040c9f1999c8e4cec27d49e7dc1149ff Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Mon, 29 Oct 2018 05:35:16 -0500 Subject: [PATCH 03/20] add argument is_bot in fixture_bot_user --- spec/minitest_helper.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/minitest_helper.rb b/spec/minitest_helper.rb index a67ce6f..3519efc 100644 --- a/spec/minitest_helper.rb +++ b/spec/minitest_helper.rb @@ -20,7 +20,8 @@ def fixture_bot_user TelegramBot::User.new( id: ENV['TEST_BOT_ID'], first_name: "telegram-bot-gem-test", - username: "gem_test_bot" + username: "gem_test_bot", + is_bot: true ) end end From 67256ecb33c52a19736df0ac55e415437ae1ee37 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Mon, 29 Oct 2018 06:50:54 -0500 Subject: [PATCH 04/20] add MessageEntity --- README.md | 8 +++--- example/bot.rb | 8 +++--- lib/telegram_bot.rb | 1 + lib/telegram_bot/message.rb | 17 +++++++++++-- lib/telegram_bot/message_entity.rb | 39 ++++++++++++++++++++++++++++++ spec/telegram_bot_spec.rb | 6 +++++ 6 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 lib/telegram_bot/message_entity.rb diff --git a/README.md b/README.md index 26f7dec..1c02a6b 100644 --- a/README.md +++ b/README.md @@ -28,15 +28,15 @@ require 'telegram_bot' bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]') bot.get_updates(fail_silently: true) do |message| - puts "@#{message.from.username}: #{message.text}" - command = message.get_command_for(bot) + msg_text = message.text + puts "@#{message.from.username}: #{msg_text}" message.reply do |reply| - case command + case msg_text when /greet/i reply.text = "Hello, #{message.from.first_name}!" else - reply.text = "#{message.from.first_name}, have no idea what #{command.inspect} means." + reply.text = "#{message.from.first_name}, have no idea what #{msg_text.inspect} means." end puts "sending #{reply.text.inspect} to @#{message.from.username}" reply.send_with(bot) diff --git a/example/bot.rb b/example/bot.rb index a3eed17..a2e611d 100644 --- a/example/bot.rb +++ b/example/bot.rb @@ -8,15 +8,15 @@ logger.debug "starting telegram bot" bot.get_updates(fail_silently: true) do |message| - logger.info "@#{message.from.username}: #{message.text}" - command = message.get_command_for(bot) + msg_text = message.text + logger.info "@#{message.from.username}: #{msg_text}" message.reply do |reply| - case command + case msg_text when /greet/i reply.text = "Hello, #{message.from.first_name}!" else - reply.text = "#{message.from.first_name}, have no idea what #{command.inspect} means." + reply.text = "#{message.from.first_name}, have no idea what #{msg_text.inspect} means." end logger.info "sending #{reply.text.inspect} to @#{message.from.username}" reply.send_with(bot) diff --git a/lib/telegram_bot.rb b/lib/telegram_bot.rb index 6e9b937..9bc7e32 100644 --- a/lib/telegram_bot.rb +++ b/lib/telegram_bot.rb @@ -8,6 +8,7 @@ require "telegram_bot/user" require "telegram_bot/group_chat" require "telegram_bot/channel" +require "telegram_bot/message_entity" require "telegram_bot/message" require "telegram_bot/keyboard" require "telegram_bot/reply_keyboard_hide" diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index 078edd6..c6f0a88 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -10,6 +10,7 @@ class Message attribute :date, DateTime attribute :chat, Channel attribute :reply_to_message, Message + attribute :entities, Array[MessageEntity] def reply(&block) reply = OutMessage.new(chat: chat) @@ -17,8 +18,20 @@ def reply(&block) reply end - def get_command_for(bot) - text && text.sub(Regexp.new("@#{bot.identity.username}($|\s|\.|,)", Regexp::IGNORECASE), '').strip + MessageEntity::TYPES_ENTITY.each do |method_name| + singular_name_of_method = "get_#{method_name}" + is_type_method = "is_#{method_name}?".to_sym + + define_method("#{singular_name_of_method}s") do + return [] if entities.nil? + entities.select(&is_type_method).map { |entity| text[entity.offset..-1] } + end + + define_method(singular_name_of_method) do + return nil if entities.nil? + entity = entities.find(&is_type_method) + text[entity.offset..-1] unless entity.nil? + end end end end diff --git a/lib/telegram_bot/message_entity.rb b/lib/telegram_bot/message_entity.rb new file mode 100644 index 0000000..92b0159 --- /dev/null +++ b/lib/telegram_bot/message_entity.rb @@ -0,0 +1,39 @@ +module TelegramBot + class MessageEntity + include Virtus.model + + MENTION = "mention" + HASHTAG = "hashtag" + CASHTAG = "cashtag" + BOT_COMMAND = "bot_command" + URL = "url" + EMAIL = "email" + PHONE_NUMBER = "phone_number" + BOLD = "bold" + ITALIC = "italic" + CODE = "code" + PRE = "pre" + TEXT_LINK = "text_link" + TEXT_MENTION = "text_mention" + TYPES_ENTITY = [ + MENTION, HASHTAG, CASHTAG, BOT_COMMAND, URL, EMAIL, + PHONE_NUMBER, BOLD, ITALIC, CODE, PRE, TEXT_LINK, TEXT_MENTION + ] + + attribute :type, String + attribute :offset, Integer + attribute :length, Integer + attribute :url, String + attribute :user, User + + def is_type?(type_entity) + type == type_entity + end + + TYPES_ENTITY.each do |method_name| + define_method("is_#{method_name}?") do + is_type?(method_name) + end + end + end +end \ No newline at end of file diff --git a/spec/telegram_bot_spec.rb b/spec/telegram_bot_spec.rb index e561684..58107e1 100644 --- a/spec/telegram_bot_spec.rb +++ b/spec/telegram_bot_spec.rb @@ -20,8 +20,10 @@ def test_integration bot = new_test_bot messages = bot.get_updates message = messages.first + entity = message.entities.first assert_equal "/start", message.text + assert_equal "/start", message.get_bot_command answer = message.reply do |reply| reply.text = "Hello, #{message.from.first_name}!" @@ -31,8 +33,12 @@ def test_integration assert_equal result.text, reply.text end + assert !message.from.is_bot? + assert_includes ["enCA", nil], message.from.language_code assert_equal message.from.id, answer.chat.id assert_equal "Hello, José!", answer.text + + assert entity.is_bot_command? end end end From deb72b0b957bf6728d7276f88e77cdbd342f7ed3 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Mon, 29 Oct 2018 18:56:09 -0500 Subject: [PATCH 05/20] add new attibutes to Message --- lib/telegram_bot/message.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index c6f0a88..acfdea9 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -6,11 +6,32 @@ class Message alias_method :to_i, :id attribute :from, User alias_method :user, :from - attribute :text, String attribute :date, DateTime attribute :chat, Channel + attribute :forward_from, User + attribute :forward_from_chat, Channel + attribute :forward_from_message_id, Integer + attribute :forward_signature, String + attribute :forward_date, DateTime attribute :reply_to_message, Message + attribute :edit_date, DateTime + attribute :media_group_id, String + attribute :author_signature, String + attribute :text, String attribute :entities, Array[MessageEntity] + attribute :caption_entities, Array[MessageEntity] + attribute :caption, String + attribute :new_chat_members, Array[User] + attribute :left_chat_member, User + attribute :new_chat_title, String + attribute :delete_chat_photo, Boolean, default: false + attribute :group_chat_created, Boolean, default: false + attribute :supergroup_chat_created, Boolean, default: false + attribute :channel_chat_created, Boolean, default: false + attribute :migrate_to_chat_id, Integer + attribute :migrate_from_chat_id, Integer + attribute :pinned_message, Message + attribute :connected_website, String def reply(&block) reply = OutMessage.new(chat: chat) From df999f8952fdc467d49c235025ef6f2077255501 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Tue, 30 Oct 2018 19:30:32 -0500 Subject: [PATCH 06/20] add new update types --- lib/telegram_bot/bot.rb | 2 +- lib/telegram_bot/update.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/telegram_bot/bot.rb b/lib/telegram_bot/bot.rb index 875d589..b0c5911 100644 --- a/lib/telegram_bot/bot.rb +++ b/lib/telegram_bot/bot.rb @@ -86,7 +86,7 @@ def get_last_updates(opts = {}) end def get_last_messages(opts = {}) - get_last_updates(opts).map(&:message) + get_last_updates(opts).map(&:get_message) end end end diff --git a/lib/telegram_bot/update.rb b/lib/telegram_bot/update.rb index 2d05b36..27eac5f 100644 --- a/lib/telegram_bot/update.rb +++ b/lib/telegram_bot/update.rb @@ -5,5 +5,12 @@ class Update alias_method :id, :update_id alias_method :to_i, :id attribute :message, Message + attribute :edited_message, Message + attribute :channel_post, Message + attribute :edited_channel_post, Message + + def get_message + message || edited_message || channel_post || edited_channel_post + end end end From 27d9f9d6b4e6a795174242e356a67cbb75a1a19c Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Wed, 31 Oct 2018 03:49:45 -0500 Subject: [PATCH 07/20] Client was changed by Connection --- fixtures/vcr_cassettes/get_me.yml | 2 +- fixtures/vcr_cassettes/integration_test.yml | 4 +-- lib/telegram_bot.rb | 10 +----- lib/telegram_bot/bot.rb | 39 ++++++++++++--------- lib/telegram_bot/client.rb | 22 ------------ lib/telegram_bot/connection.rb | 20 +++++++++++ 6 files changed, 47 insertions(+), 50 deletions(-) delete mode 100644 lib/telegram_bot/client.rb create mode 100644 lib/telegram_bot/connection.rb diff --git a/fixtures/vcr_cassettes/get_me.yml b/fixtures/vcr_cassettes/get_me.yml index 59b25a8..1cea15f 100644 --- a/fixtures/vcr_cassettes/get_me.yml +++ b/fixtures/vcr_cassettes/get_me.yml @@ -1,7 +1,7 @@ --- http_interactions: - request: - method: post + method: get uri: https://api.telegram.org/botTEST_BOT_API_TOKEN/getMe body: encoding: US-ASCII diff --git a/fixtures/vcr_cassettes/integration_test.yml b/fixtures/vcr_cassettes/integration_test.yml index 83237fa..f5c2fbd 100644 --- a/fixtures/vcr_cassettes/integration_test.yml +++ b/fixtures/vcr_cassettes/integration_test.yml @@ -1,7 +1,7 @@ --- http_interactions: - request: - method: post + method: get uri: https://api.telegram.org/botTEST_BOT_API_TOKEN/getUpdates?offset=0 body: encoding: US-ASCII @@ -81,7 +81,7 @@ http_interactions: http_version: recorded_at: Mon, 06 Aug 2018 22:25:44 GMT - request: - method: post + method: get uri: https://api.telegram.org/botTEST_BOT_API_TOKEN/getMe body: encoding: US-ASCII diff --git a/lib/telegram_bot.rb b/lib/telegram_bot.rb index 9bc7e32..7bc2aed 100644 --- a/lib/telegram_bot.rb +++ b/lib/telegram_bot.rb @@ -18,19 +18,11 @@ require "telegram_bot/update" require "telegram_bot/api_response" require "telegram_bot/bot" -require "telegram_bot/client" +require "telegram_bot/connection" module TelegramBot def self.new(opts) - # compatibility with just passing a token - if opts.is_a?(String) - opts = { token: opts } - end - - opts[:logger] ||= NullLogger.new - opts[:client] ||= Client.new(token: opts.fetch(:token), logger: opts[:logger], proxy: opts[:proxy]) - Bot.new(opts) end end diff --git a/lib/telegram_bot/bot.rb b/lib/telegram_bot/bot.rb index b0c5911..4f7357d 100644 --- a/lib/telegram_bot/bot.rb +++ b/lib/telegram_bot/bot.rb @@ -17,18 +17,24 @@ def to_h end class Bot + ENDPOINT = 'https://api.telegram.org/' + attr_reader :connection + def initialize(opts = {}) + # compatibility with just passing a token + opts = {token: opts} if opts.is_a?(String) + + @token = opts.fetch(:token) + @base_path = "/bot#{@token}" @offset = opts[:offset] || 0 - @logger = opts.fetch(:logger) - @client = opts.fetch(:client) + @logger = opts[:logger] || NullLogger.new + @connection = Connection.new(ENDPOINT, persistent: true, proxy: opts[:proxy]) end def get_me - @me ||= @client - .request(:getMe) - .and_then do |result| - User.new(result) - end + @me ||= @connection + .get(path: "#{@base_path}/getMe") + .and_then { |result| User.new(result) } .value! end alias_method :me, :get_me @@ -51,18 +57,18 @@ def get_updates(opts = {}, &block) def send_message(out_message) logger.info "sending message: #{out_message.text.inspect} to #{out_message.chat_friendly_name}" - @client - .request(:sendMessage, out_message) - .and_then do |result| - Message.new(result) - end + path = "#{@base_path}/sendMessage" + @connection + .post(path: path, query: out_message.to_h) + .and_then { |result| Message.new(result) } .value! end def set_webhook(url, allowed_updates: %i(message)) logger.info "setting webhook url to #{url}, allowed_updates: #{allowed_updates}" - request = WebhookRequest.new(url: url, allowed_updates: allowed_updates) - @client.request(:setWebhook, request) + webhook_request = WebhookRequest.new(url: url, allowed_updates: allowed_updates) + path = "#{@base_path}/setWebhook" + @connection.post(path: path, query: webhook_request.to_h) end def remove_webhook @@ -74,8 +80,9 @@ def remove_webhook def get_last_updates(opts = {}) opts[:offset] ||= @offset - request = UpdatesRequest.new(opts) - response = @client.request(:getUpdates, request) + updates_request = UpdatesRequest.new(opts) + path = "#{@base_path}/getUpdates" + response = @connection.get(path: path, query: updates_request.to_h) if opts[:fail_silently] && !response.ok? logger.warn "error when getting updates. ignoring due to fail_silently." return [] diff --git a/lib/telegram_bot/client.rb b/lib/telegram_bot/client.rb deleted file mode 100644 index 821164a..0000000 --- a/lib/telegram_bot/client.rb +++ /dev/null @@ -1,22 +0,0 @@ -module TelegramBot - class Client - ENDPOINT = 'https://api.telegram.org/' - - def initialize(token:, logger:, proxy: nil) - @token = token - @logger = logger - @proxy = proxy - @connection = Excon.new(ENDPOINT, persistent: true, proxy: @proxy) - end - - def request(action, query = {}) - path = "/bot#{@token}/#{action}" - res = @connection.post(path: path, query: query.to_h) - ApiResponse.from_excon(res) - end - - private - attr_reader :token - attr_reader :logger - end -end diff --git a/lib/telegram_bot/connection.rb b/lib/telegram_bot/connection.rb new file mode 100644 index 0000000..b6bd2f9 --- /dev/null +++ b/lib/telegram_bot/connection.rb @@ -0,0 +1,20 @@ +module TelegramBot + class Connection + attr_reader :connection + + def initialize(url, params = {}) + @connection = Excon.new(url, params) + end + + def request(params = {}, &block) + response = @connection.request(params, &block) + ApiResponse.from_excon(response) + end + + Excon::HTTP_VERBS.each do |method| + define_method(method) do |params = {}, &block| + request(params.merge(method: method), &block) + end + end + end +end From 2f9f5fa0bf76f845f95139f3e599612706c0247d Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Wed, 31 Oct 2018 04:05:21 -0500 Subject: [PATCH 08/20] add full_name method --- lib/telegram_bot/user.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/telegram_bot/user.rb b/lib/telegram_bot/user.rb index 6adab27..d34dace 100644 --- a/lib/telegram_bot/user.rb +++ b/lib/telegram_bot/user.rb @@ -6,13 +6,17 @@ class User attribute :first_name, String attribute :last_name, String attribute :username, String - attribute :is_bot, Boolean, default_value: false + attribute :is_bot, Boolean attribute :language_code, String def ==(other) other.is_a?(self.class) && other.hash == hash end + def full_name + @last_name ? "#{@first_name} #{@last_name}" : @first_name + end + def hash to_h.hash end From 2a3e14e9799124dcab23bbb218a4cc8d45461f4b Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Fri, 2 Nov 2018 01:27:55 -0500 Subject: [PATCH 09/20] Fix bug in loop --- lib/telegram_bot/bot.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/telegram_bot/bot.rb b/lib/telegram_bot/bot.rb index 4f7357d..359812d 100644 --- a/lib/telegram_bot/bot.rb +++ b/lib/telegram_bot/bot.rb @@ -47,6 +47,7 @@ def get_updates(opts = {}, &block) logger.info "starting get_updates loop" loop do messages = get_last_messages(opts) + opts[:offset] = @offset messages.compact.each do |message| next unless message logger.info "message from @#{message.chat.friendly_name}: #{message.text.inspect}" @@ -87,7 +88,7 @@ def get_last_updates(opts = {}) logger.warn "error when getting updates. ignoring due to fail_silently." return [] end - updates = response.value!.compact.map{|raw_update| Update.new(raw_update) } + updates = response.value!.compact.map { |raw_update| Update.new(raw_update) } @offset = updates.last.id + 1 if updates.any? updates end From 59a583d93795b6a163a35c440d0818817802f2af Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Sun, 11 Nov 2018 21:35:05 -0500 Subject: [PATCH 10/20] add all_entities to Message --- lib/telegram_bot/connection.rb | 6 +++--- lib/telegram_bot/message.rb | 32 ++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/telegram_bot/connection.rb b/lib/telegram_bot/connection.rb index b6bd2f9..9f824b5 100644 --- a/lib/telegram_bot/connection.rb +++ b/lib/telegram_bot/connection.rb @@ -11,9 +11,9 @@ def request(params = {}, &block) ApiResponse.from_excon(response) end - Excon::HTTP_VERBS.each do |method| - define_method(method) do |params = {}, &block| - request(params.merge(method: method), &block) + Excon::HTTP_VERBS.each do |method_name| + define_method(method_name) do |params = {}, &block| + request(params.merge(method: method_name), &block) end end end diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index acfdea9..f7cc0a4 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -39,20 +39,28 @@ def reply(&block) reply end - MessageEntity::TYPES_ENTITY.each do |method_name| - singular_name_of_method = "get_#{method_name}" - is_type_method = "is_#{method_name}?".to_sym + def all_entities + (entities || []) + (caption_entities || []) + end - define_method("#{singular_name_of_method}s") do - return [] if entities.nil? - entities.select(&is_type_method).map { |entity| text[entity.offset..-1] } - end + MessageEntity::TYPES_ENTITY.each do |method_name| + class_eval <<-DEF, __FILE__, __LINE__ + 1 + def get_#{method_name}s(return_entities=false) + return [] unless all_entities.any? + list_entities = all_entities.select(&:is_#{method_name}?) + return list_entities if return_entities + list_entities.map { |entity| entity.get_#{method_name}(self) } + end + DEF - define_method(singular_name_of_method) do - return nil if entities.nil? - entity = entities.find(&is_type_method) - text[entity.offset..-1] unless entity.nil? - end + class_eval <<-DEF, __FILE__, __LINE__ + 1 + def get_#{method_name}(return_entity=false) + return nil unless all_entities.any? + entity = all_entities.find(&:is_#{method_name}?) + return entity if return_entity + entity.get_#{method_name}(self) if entity + end + DEF end end end From 9a27fe120dda31b076a236c98248e1cc93eb5876 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Fri, 22 Feb 2019 21:14:54 -0500 Subject: [PATCH 11/20] replace query by body --- lib/telegram_bot/bot.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/telegram_bot/bot.rb b/lib/telegram_bot/bot.rb index 359812d..d1a13cb 100644 --- a/lib/telegram_bot/bot.rb +++ b/lib/telegram_bot/bot.rb @@ -60,7 +60,7 @@ def send_message(out_message) logger.info "sending message: #{out_message.text.inspect} to #{out_message.chat_friendly_name}" path = "#{@base_path}/sendMessage" @connection - .post(path: path, query: out_message.to_h) + .post(path: path, body: URI.encode_www_form(out_message.to_h)) .and_then { |result| Message.new(result) } .value! end @@ -69,7 +69,7 @@ def set_webhook(url, allowed_updates: %i(message)) logger.info "setting webhook url to #{url}, allowed_updates: #{allowed_updates}" webhook_request = WebhookRequest.new(url: url, allowed_updates: allowed_updates) path = "#{@base_path}/setWebhook" - @connection.post(path: path, query: webhook_request.to_h) + @connection.post(path: path, body: URI.encode_www_form(webhook_request.to_h)) end def remove_webhook From fd00985e53ad92a488d6af5acb5801cabd56aff2 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Fri, 22 Feb 2019 21:15:46 -0500 Subject: [PATCH 12/20] used class_eval --- lib/telegram_bot/message_entity.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/telegram_bot/message_entity.rb b/lib/telegram_bot/message_entity.rb index 92b0159..e3055a2 100644 --- a/lib/telegram_bot/message_entity.rb +++ b/lib/telegram_bot/message_entity.rb @@ -31,9 +31,19 @@ def is_type?(type_entity) end TYPES_ENTITY.each do |method_name| - define_method("is_#{method_name}?") do - is_type?(method_name) - end + class_eval <<-DEF, __FILE__, __LINE__ + 1 + def is_#{method_name}? + is_type?("#{method_name}") + end + DEF + + class_eval <<-DEF, __FILE__, __LINE__ + 1 + def get_#{method_name}(message, only_#{method_name}: false) + limit = -1 + limit += offset + length if only_#{method_name} + message.text[offset..limit] if is_#{method_name}? + end + DEF end end end \ No newline at end of file From 6fd6d09aaf0dfa1c1eeeb0dedd22d4e9ceaa3699 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Fri, 22 Feb 2019 21:18:33 -0500 Subject: [PATCH 13/20] rename TYPES_ENTITY to ENTITY_TYPES --- lib/telegram_bot/message.rb | 2 +- lib/telegram_bot/message_entity.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index f7cc0a4..b9a3d5d 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -43,7 +43,7 @@ def all_entities (entities || []) + (caption_entities || []) end - MessageEntity::TYPES_ENTITY.each do |method_name| + MessageEntity::ENTITY_TYPES.each do |method_name| class_eval <<-DEF, __FILE__, __LINE__ + 1 def get_#{method_name}s(return_entities=false) return [] unless all_entities.any? diff --git a/lib/telegram_bot/message_entity.rb b/lib/telegram_bot/message_entity.rb index e3055a2..8421e3c 100644 --- a/lib/telegram_bot/message_entity.rb +++ b/lib/telegram_bot/message_entity.rb @@ -15,7 +15,7 @@ class MessageEntity PRE = "pre" TEXT_LINK = "text_link" TEXT_MENTION = "text_mention" - TYPES_ENTITY = [ + ENTITY_TYPES = [ MENTION, HASHTAG, CASHTAG, BOT_COMMAND, URL, EMAIL, PHONE_NUMBER, BOLD, ITALIC, CODE, PRE, TEXT_LINK, TEXT_MENTION ] @@ -30,7 +30,7 @@ def is_type?(type_entity) type == type_entity end - TYPES_ENTITY.each do |method_name| + ENTITY_TYPES.each do |method_name| class_eval <<-DEF, __FILE__, __LINE__ + 1 def is_#{method_name}? is_type?("#{method_name}") From 5ed9d1492cc131b3a30e26e7943ba045b1be61ef Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Fri, 22 Feb 2019 22:02:12 -0500 Subject: [PATCH 14/20] rename Channel to Chat --- README.md | 10 +++++----- lib/telegram_bot.rb | 2 +- lib/telegram_bot/{channel.rb => chat.rb} | 4 ++-- lib/telegram_bot/message.rb | 4 ++-- lib/telegram_bot/out_message.rb | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) rename lib/telegram_bot/{channel.rb => chat.rb} (69%) diff --git a/README.md b/README.md index 1c02a6b..36e55ed 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,8 @@ message.from.first_name # "Homer" message.from.last_name # "Simpson" message.from.username # "mr_x" -# channel -message.channel.id # 123123123 (telegram's id) +# chat +message.chat.id # 123123123 (telegram's id) # reply message.reply do |reply| @@ -110,13 +110,13 @@ reply.text = "i'll do it after going to moe's" bot.send_message(reply) ``` -To send message to specific channel you could do following: +To send message to specific chat you could do following: ```ruby bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]') -channel = TelegramBot::Channel.new(id: channel_id) +chat = TelegramBot::Chat.new(id: chat_id) message = TelegramBot::OutMessage.new -message.chat = channel +message.chat = chat message.text = 'Some message' message.send_with(bot) diff --git a/lib/telegram_bot.rb b/lib/telegram_bot.rb index 7bc2aed..4d971ec 100644 --- a/lib/telegram_bot.rb +++ b/lib/telegram_bot.rb @@ -7,7 +7,7 @@ require "telegram_bot/null_logger" require "telegram_bot/user" require "telegram_bot/group_chat" -require "telegram_bot/channel" +require "telegram_bot/chat" require "telegram_bot/message_entity" require "telegram_bot/message" require "telegram_bot/keyboard" diff --git a/lib/telegram_bot/channel.rb b/lib/telegram_bot/chat.rb similarity index 69% rename from lib/telegram_bot/channel.rb rename to lib/telegram_bot/chat.rb index d4baf89..40d967a 100644 --- a/lib/telegram_bot/channel.rb +++ b/lib/telegram_bot/chat.rb @@ -1,12 +1,12 @@ module TelegramBot - class Channel + class Chat include Virtus.model attribute :id, Integer attribute :username, String attribute :title, String def friendly_name - username ? "@#{username}" : "channel #{title.inspect}" + username ? "@#{username}" : "chat #{title.inspect}" end end end diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index b9a3d5d..f81ff36 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -7,9 +7,9 @@ class Message attribute :from, User alias_method :user, :from attribute :date, DateTime - attribute :chat, Channel + attribute :chat, Chat attribute :forward_from, User - attribute :forward_from_chat, Channel + attribute :forward_from_chat, Chat attribute :forward_from_message_id, Integer attribute :forward_signature, String attribute :forward_date, DateTime diff --git a/lib/telegram_bot/out_message.rb b/lib/telegram_bot/out_message.rb index e2e0591..4684579 100644 --- a/lib/telegram_bot/out_message.rb +++ b/lib/telegram_bot/out_message.rb @@ -1,7 +1,7 @@ module TelegramBot class OutMessage include Virtus.model - attribute :chat, Channel + attribute :chat, Chat attribute :text, String attribute :reply_to, Message attribute :parse_mode, String From 54d422db484bdb85ce888e4ec5b44f45f92bc78e Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Fri, 22 Feb 2019 23:03:01 -0500 Subject: [PATCH 15/20] the query was moved to the body of the request --- fixtures/vcr_cassettes/integration_test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fixtures/vcr_cassettes/integration_test.yml b/fixtures/vcr_cassettes/integration_test.yml index f5c2fbd..56200a0 100644 --- a/fixtures/vcr_cassettes/integration_test.yml +++ b/fixtures/vcr_cassettes/integration_test.yml @@ -44,10 +44,10 @@ http_interactions: recorded_at: Mon, 06 Aug 2018 22:25:44 GMT - request: method: post - uri: https://api.telegram.org/botTEST_BOT_API_TOKEN/sendMessage?chat_id=5678&text=Hello%2C+Jos%C3%A9%21 + uri: https://api.telegram.org/botTEST_BOT_API_TOKEN/sendMessage body: encoding: US-ASCII - string: '' + string: 'chat_id=5678&text=Hello%2C+Jos%C3%A9%21' headers: User-Agent: - excon/0.61.0 From e37d6bd1afa2dec48ab7c9eadc04d24b741c24c7 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Wed, 27 Feb 2019 08:46:46 -0500 Subject: [PATCH 16/20] added new attributes to Chat --- lib/telegram_bot.rb | 40 +++++++++++++++++++++------------------- lib/telegram_bot/chat.rb | 11 ++++++++++- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/telegram_bot.rb b/lib/telegram_bot.rb index 4d971ec..4a59c1f 100644 --- a/lib/telegram_bot.rb +++ b/lib/telegram_bot.rb @@ -2,26 +2,28 @@ require 'virtus' require 'json' -require "telegram_bot/version" -require "telegram_bot/result" -require "telegram_bot/null_logger" -require "telegram_bot/user" -require "telegram_bot/group_chat" -require "telegram_bot/chat" -require "telegram_bot/message_entity" -require "telegram_bot/message" -require "telegram_bot/keyboard" -require "telegram_bot/reply_keyboard_hide" -require "telegram_bot/reply_keyboard_markup" -require "telegram_bot/force_replay" -require "telegram_bot/out_message" -require "telegram_bot/update" -require "telegram_bot/api_response" -require "telegram_bot/bot" -require "telegram_bot/connection" - - module TelegramBot + { + Version: "version", + Result: "result", + NullLogger: "null_logger", + User: "user", + Chat: "chat", + MessageEntity: "message_entity", + Message: "message", + Keyboard: "keyboard", + ReplyKeyboardHide: "reply_keyboard_hide", + ReplyKeyboardMarkup: "reply_keyboard_markup", + ForceReplay: "force_replay", + OutMessage: "out_message", + Update: "update", + ApiResponse: "api_response", + Bot: "bot", + Connection: "connection", + }.each do |key, val| + autoload(key, "telegram_bot/#{val}") + end + def self.new(opts) Bot.new(opts) end diff --git a/lib/telegram_bot/chat.rb b/lib/telegram_bot/chat.rb index 40d967a..43fb4d7 100644 --- a/lib/telegram_bot/chat.rb +++ b/lib/telegram_bot/chat.rb @@ -2,8 +2,17 @@ module TelegramBot class Chat include Virtus.model attribute :id, Integer - attribute :username, String + attribute :type, String attribute :title, String + attribute :username, String + attribute :first_name, String + attribute :last_name, String + attribute :all_members_are_administrators, Boolean + attribute :description, String + attribute :invite_link, String + attribute :pinned_message, Message + attribute :sticker_set_name, String + attribute :can_set_sticker_set, Boolean def friendly_name username ? "@#{username}" : "chat #{title.inspect}" From d119e69888d557e23e3b5531fe9a236eaf05996a Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Tue, 12 Mar 2019 21:22:05 -0500 Subject: [PATCH 17/20] add the private post_message method and remove out_message.rb --- README.md | 26 +++++----- lib/telegram_bot.rb | 1 - lib/telegram_bot/bot.rb | 84 +++++++++++++++++++++++---------- lib/telegram_bot/message.rb | 4 +- lib/telegram_bot/out_message.rb | 33 ------------- spec/telegram_bot_spec.rb | 10 ++-- 6 files changed, 76 insertions(+), 82 deletions(-) delete mode 100644 lib/telegram_bot/out_message.rb diff --git a/README.md b/README.md index 36e55ed..e5e2d9e 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,12 @@ bot.get_updates(fail_silently: true) do |message| message.reply do |reply| case msg_text when /greet/i - reply.text = "Hello, #{message.from.first_name}!" + reply[:text] = "Hello, #{message.from.first_name}!" else - reply.text = "#{message.from.first_name}, have no idea what #{msg_text.inspect} means." + reply[:text] = "#{message.from.first_name}, have no idea what #{msg_text.inspect} means." end - puts "sending #{reply.text.inspect} to @#{message.from.username}" - reply.send_with(bot) + puts "sending #{reply[:text].inspect} to @#{message.from.username}" + bot.send_message(**reply) end end ``` @@ -101,32 +101,28 @@ message.chat.id # 123123123 (telegram's id) # reply message.reply do |reply| - reply.text = "homer please clean the garage" - reply.send_with(bot) + reply[:text] = "homer please clean the garage" + bot.send_message(**reply) end # or reply = message.reply -reply.text = "i'll do it after going to moe's" -bot.send_message(reply) +reply[:text] = "i'll do it after going to moe's" +bot.send_message(**reply) ``` To send message to specific chat you could do following: ```ruby bot = TelegramBot.new(token: '[YOUR TELEGRAM BOT TOKEN GOES HERE]') -chat = TelegramBot::Chat.new(id: chat_id) -message = TelegramBot::OutMessage.new -message.chat = chat -message.text = 'Some message' - -message.send_with(bot) +message = {chat_id: chat_id, text: 'Some message'} +bot.send_message(**message) ``` Also you may pass additional options described in [API Docs](https://core.telegram.org/bots/api#sendmessage) ```ruby -message.parse_mode = 'Markdown' +message[:parse_mode] = 'Markdown' ``` ## Contributing diff --git a/lib/telegram_bot.rb b/lib/telegram_bot.rb index 4a59c1f..b4daac2 100644 --- a/lib/telegram_bot.rb +++ b/lib/telegram_bot.rb @@ -15,7 +15,6 @@ module TelegramBot ReplyKeyboardHide: "reply_keyboard_hide", ReplyKeyboardMarkup: "reply_keyboard_markup", ForceReplay: "force_replay", - OutMessage: "out_message", Update: "update", ApiResponse: "api_response", Bot: "bot", diff --git a/lib/telegram_bot/bot.rb b/lib/telegram_bot/bot.rb index d1a13cb..36ad131 100644 --- a/lib/telegram_bot/bot.rb +++ b/lib/telegram_bot/bot.rb @@ -56,20 +56,27 @@ def get_updates(opts = {}, &block) end end - def send_message(out_message) - logger.info "sending message: #{out_message.text.inspect} to #{out_message.chat_friendly_name}" - path = "#{@base_path}/sendMessage" - @connection - .post(path: path, body: URI.encode_www_form(out_message.to_h)) - .and_then { |result| Message.new(result) } - .value! + # send_message(text:, chat_id:, parse_mode: nil, disable_web_page_preview: nil, **kwargs) + def send_message(*args, **kwargs) + text = kwargs.fetch(:text) { args.fetch(0) } + chat_id = kwargs.fetch(:chat_id) { args.fetch(1) } + parse_mode = kwargs.fetch(:parse_mode) { args[2] } + disable_web_page_preview = kwargs.fetch(:disable_web_page_preview) { args[3] } + + logger.info "sending message: #{text.inspect}" + data = {text: text, chat_id: chat_id} + data[:parse_mode] = parse_mode unless parse_mode.nil? + data[:disable_web_page_preview] = disable_web_page_preview unless disable_web_page_preview.nil? + + args.shift(4) + args.unshift("#{@base_path}/sendMessage", data) + Message.new(post_message(*args, **kwargs)) end def set_webhook(url, allowed_updates: %i(message)) logger.info "setting webhook url to #{url}, allowed_updates: #{allowed_updates}" webhook_request = WebhookRequest.new(url: url, allowed_updates: allowed_updates) - path = "#{@base_path}/setWebhook" - @connection.post(path: path, body: URI.encode_www_form(webhook_request.to_h)) + post_message(path: "#{@base_path}/setWebhook", data: webhook_request.to_h) end def remove_webhook @@ -77,24 +84,49 @@ def remove_webhook end private - attr_reader :logger - - def get_last_updates(opts = {}) - opts[:offset] ||= @offset - updates_request = UpdatesRequest.new(opts) - path = "#{@base_path}/getUpdates" - response = @connection.get(path: path, query: updates_request.to_h) - if opts[:fail_silently] && !response.ok? - logger.warn "error when getting updates. ignoring due to fail_silently." - return [] + attr_reader :logger + + def get_last_updates(opts = {}) + opts[:offset] ||= @offset + updates_request = UpdatesRequest.new(opts) + path = "#{@base_path}/getUpdates" + response = @connection.get(path: path, query: updates_request.to_h) + if opts[:fail_silently] && !response.ok? + logger.warn "error when getting updates. ignoring due to fail_silently." + return [] + end + updates = response.value!.compact.map { |raw_update| Update.new(raw_update) } + @offset = updates.last.id + 1 if updates.any? + updates end - updates = response.value!.compact.map { |raw_update| Update.new(raw_update) } - @offset = updates.last.id + 1 if updates.any? - updates - end - def get_last_messages(opts = {}) - get_last_updates(opts).map(&:get_message) - end + def get_last_messages(opts = {}) + get_last_updates(opts).map(&:get_message) + end + + # post_message(path:, data: {}, disable_notification: nil, reply_to_message_id: nil, content_type: nil) + def post_message(*args, **kwargs) + path = kwargs.fetch(:path) { args.fetch(0) } + data = kwargs.fetch(:data) { args.fetch(1, {}) } + disable_notification = kwargs.fetch(:disable_notification) { args[2] } + reply_to_message_id = kwargs.fetch(:reply_to_message_id) { args[3] } + content_type = kwargs.fetch(:content_type) { args[4] } + + data[:disable_notification] = disable_notification unless disable_notification.nil? + data[:reply_to_message_id] = reply_to_message_id unless reply_to_message_id.nil? + + if content_type.nil? + content_type = "application/x-www-form-urlencoded" + data = URI.encode_www_form(data) + else + content_type = content_type.downcase + end + + if content_type == "application/json" + data = JSON.dump(data) + end + + @connection.post(path: path, body: data, headers: {"Content-Type" => content_type}).value! + end end end diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index f81ff36..e3c89ab 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -33,8 +33,8 @@ class Message attribute :pinned_message, Message attribute :connected_website, String - def reply(&block) - reply = OutMessage.new(chat: chat) + def reply + reply = {chat_id: chat.id, reply_to_message_id: message_id} yield reply if block_given? reply end diff --git a/lib/telegram_bot/out_message.rb b/lib/telegram_bot/out_message.rb deleted file mode 100644 index 4684579..0000000 --- a/lib/telegram_bot/out_message.rb +++ /dev/null @@ -1,33 +0,0 @@ -module TelegramBot - class OutMessage - include Virtus.model - attribute :chat, Chat - attribute :text, String - attribute :reply_to, Message - attribute :parse_mode, String - attribute :disable_web_page_preview, Boolean - attribute :reply_markup, Keyboard - - def send_with(bot) - bot.send_message(self) - end - - def chat_friendly_name - chat.friendly_name - end - - def to_h - message = { - text: text, - chat_id: chat.id - } - - message[:reply_to_message_id] = reply_to.id unless reply_to.nil? - message[:parse_mode] = parse_mode unless parse_mode.nil? - message[:disable_web_page_preview] = disable_web_page_preview unless disable_web_page_preview.nil? - message[:reply_markup] = reply_markup.to_h.to_json unless reply_markup.nil? - - message - end - end -end diff --git a/spec/telegram_bot_spec.rb b/spec/telegram_bot_spec.rb index 58107e1..88147ca 100644 --- a/spec/telegram_bot_spec.rb +++ b/spec/telegram_bot_spec.rb @@ -26,17 +26,17 @@ def test_integration assert_equal "/start", message.get_bot_command answer = message.reply do |reply| - reply.text = "Hello, #{message.from.first_name}!" - result = reply.send_with(bot) + reply[:text] = "Hello, #{message.from.first_name}!" + result = bot.send_message(**reply) assert_equal bot.get_me.id, result.from.id - assert_equal result.text, reply.text + assert_equal result.text, reply[:text] end assert !message.from.is_bot? assert_includes ["enCA", nil], message.from.language_code - assert_equal message.from.id, answer.chat.id - assert_equal "Hello, José!", answer.text + assert_equal message.from.id, answer[:chat_id] + assert_equal "Hello, José!", answer[:text] assert entity.is_bot_command? end From 7a1bb39b7404e0ab0eba5651147d466c371ba6ce Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Tue, 12 Mar 2019 21:23:30 -0500 Subject: [PATCH 18/20] add chat types --- lib/telegram_bot/chat.rb | 19 +++++++++++++++++++ lib/telegram_bot/group_chat.rb | 8 -------- spec/telegram_bot_spec.rb | 2 ++ 3 files changed, 21 insertions(+), 8 deletions(-) delete mode 100644 lib/telegram_bot/group_chat.rb diff --git a/lib/telegram_bot/chat.rb b/lib/telegram_bot/chat.rb index 43fb4d7..ac61a34 100644 --- a/lib/telegram_bot/chat.rb +++ b/lib/telegram_bot/chat.rb @@ -1,6 +1,13 @@ module TelegramBot class Chat include Virtus.model + + PRIVATE = "private" + GROUP = "group" + SUPERGROUP = "supergroup" + CHANNEL = "channel" + CHAT_TYPES = [PRIVATE, GROUP, SUPERGROUP, CHANNEL] + attribute :id, Integer attribute :type, String attribute :title, String @@ -17,5 +24,17 @@ class Chat def friendly_name username ? "@#{username}" : "chat #{title.inspect}" end + + def is_type?(chat_type) + type == chat_type + end + + CHAT_TYPES.each do |chat_type| + class_eval <<-DEF, __FILE__, __LINE__ + 1 + def is_#{chat_type}? + is_type?("#{chat_type}") + end + DEF + end end end diff --git a/lib/telegram_bot/group_chat.rb b/lib/telegram_bot/group_chat.rb deleted file mode 100644 index 9f742fa..0000000 --- a/lib/telegram_bot/group_chat.rb +++ /dev/null @@ -1,8 +0,0 @@ -module TelegramBot - class GroupChat - include Virtus.model - attribute :id, Integer - alias_method :to_i, :id - attribute :title, String - end -end diff --git a/spec/telegram_bot_spec.rb b/spec/telegram_bot_spec.rb index 88147ca..18488f7 100644 --- a/spec/telegram_bot_spec.rb +++ b/spec/telegram_bot_spec.rb @@ -24,6 +24,8 @@ def test_integration assert_equal "/start", message.text assert_equal "/start", message.get_bot_command + assert_equal true, message.chat.is_private? + assert_equal false, message.chat.is_group? answer = message.reply do |reply| reply[:text] = "Hello, #{message.from.first_name}!" From ae0fb92abf01fa1a7fb6c4b875b7ae157d148c76 Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Tue, 12 Mar 2019 23:33:29 -0500 Subject: [PATCH 19/20] add only_#{entity_type} parameter --- lib/telegram_bot/message.rb | 10 +++++----- lib/telegram_bot/message_entity.rb | 3 ++- spec/telegram_bot_spec.rb | 2 ++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/telegram_bot/message.rb b/lib/telegram_bot/message.rb index e3c89ab..296d7fd 100644 --- a/lib/telegram_bot/message.rb +++ b/lib/telegram_bot/message.rb @@ -45,20 +45,20 @@ def all_entities MessageEntity::ENTITY_TYPES.each do |method_name| class_eval <<-DEF, __FILE__, __LINE__ + 1 - def get_#{method_name}s(return_entities=false) + def get_#{method_name}s(return_entities: false, only_#{method_name}: false) return [] unless all_entities.any? list_entities = all_entities.select(&:is_#{method_name}?) return list_entities if return_entities - list_entities.map { |entity| entity.get_#{method_name}(self) } + list_entities.map { |entity| entity.get_#{method_name}(self, only_#{method_name}: only_#{method_name}) } end DEF class_eval <<-DEF, __FILE__, __LINE__ + 1 - def get_#{method_name}(return_entity=false) + def get_#{method_name}(return_entity: false, only_#{method_name}: false) return nil unless all_entities.any? entity = all_entities.find(&:is_#{method_name}?) - return entity if return_entity - entity.get_#{method_name}(self) if entity + return entity if return_entity || entity.nil? + entity.get_#{method_name}(self, only_#{method_name}: only_#{method_name}) end DEF end diff --git a/lib/telegram_bot/message_entity.rb b/lib/telegram_bot/message_entity.rb index 8421e3c..41647e5 100644 --- a/lib/telegram_bot/message_entity.rb +++ b/lib/telegram_bot/message_entity.rb @@ -39,9 +39,10 @@ def is_#{method_name}? class_eval <<-DEF, __FILE__, __LINE__ + 1 def get_#{method_name}(message, only_#{method_name}: false) + return nil unless is_#{method_name}? limit = -1 limit += offset + length if only_#{method_name} - message.text[offset..limit] if is_#{method_name}? + message.text[offset..limit] end DEF end diff --git a/spec/telegram_bot_spec.rb b/spec/telegram_bot_spec.rb index 18488f7..7140c1d 100644 --- a/spec/telegram_bot_spec.rb +++ b/spec/telegram_bot_spec.rb @@ -24,6 +24,8 @@ def test_integration assert_equal "/start", message.text assert_equal "/start", message.get_bot_command + assert_equal ["/start"], message.get_bot_commands + assert_nil message.get_mention assert_equal true, message.chat.is_private? assert_equal false, message.chat.is_group? From d2f37118e3d3b410266cfb90fd0d84fe89ef446f Mon Sep 17 00:00:00 2001 From: Ruben Ignacio Date: Tue, 12 Mar 2019 23:36:59 -0500 Subject: [PATCH 20/20] add send_message method --- lib/telegram_bot/bot.rb | 6 +++--- lib/telegram_bot/chat.rb | 8 ++++++++ spec/telegram_bot_spec.rb | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/telegram_bot/bot.rb b/lib/telegram_bot/bot.rb index 36ad131..473eff7 100644 --- a/lib/telegram_bot/bot.rb +++ b/lib/telegram_bot/bot.rb @@ -56,10 +56,10 @@ def get_updates(opts = {}, &block) end end - # send_message(text:, chat_id:, parse_mode: nil, disable_web_page_preview: nil, **kwargs) + # send_message(chat_id:, text:, parse_mode: nil, disable_web_page_preview: nil, **kwargs) def send_message(*args, **kwargs) - text = kwargs.fetch(:text) { args.fetch(0) } - chat_id = kwargs.fetch(:chat_id) { args.fetch(1) } + chat_id = kwargs.fetch(:chat_id) { args.fetch(0) } + text = kwargs.fetch(:text) { args.fetch(1) } parse_mode = kwargs.fetch(:parse_mode) { args[2] } disable_web_page_preview = kwargs.fetch(:disable_web_page_preview) { args[3] } diff --git a/lib/telegram_bot/chat.rb b/lib/telegram_bot/chat.rb index ac61a34..29b38e9 100644 --- a/lib/telegram_bot/chat.rb +++ b/lib/telegram_bot/chat.rb @@ -36,5 +36,13 @@ def is_#{chat_type}? end DEF end + + # send_message(bot:, text:, parse_mode: nil, disable_web_page_preview: nil, **kwargs) + def send_message(*args, **kwargs) + bot = kwargs.fetch(:bot) { args.fetch(0) } + args[0] = id + kwargs[:chat_id] = id + bot.send_message(*args, **kwargs) + end end end diff --git a/spec/telegram_bot_spec.rb b/spec/telegram_bot_spec.rb index 7140c1d..cad7cc4 100644 --- a/spec/telegram_bot_spec.rb +++ b/spec/telegram_bot_spec.rb @@ -31,7 +31,7 @@ def test_integration answer = message.reply do |reply| reply[:text] = "Hello, #{message.from.first_name}!" - result = bot.send_message(**reply) + result = message.chat.send_message(bot, **reply) assert_equal bot.get_me.id, result.from.id assert_equal result.text, reply[:text]