telegram-bot 0.14.4 → 0.15.0

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: 600d9d8e3bee50caefa1959a0334b35cd2fc36308296d66cd28be8eac6d46683
4
- data.tar.gz: db65a62e6eda50dc825c56a6eca3ff08dbaf501eab80787814d526d6caad2f69
3
+ metadata.gz: 36fa85feb323b053ce1768ed84b40d1c69d0e8fc78b5dc7aee1f699462f3b4d2
4
+ data.tar.gz: 56b342a641205785fecf430bdde6601b7105f5ec3446e45f7388fe8312f77b24
5
5
  SHA512:
6
- metadata.gz: 631e57ecb48e06319bd06084843a827f6130e65317fa87191649edaeb54615f7af9c9ceaa2a269b7fa126fd82daf18052b9640ca4b52a6b253e4a63a357efc32
7
- data.tar.gz: 6e8c874e2c12b8030b3e6fb413f2073eab28aaa81cdf030b4e87809a61556c4f6326263fe3bf2c221ff3a1132ad02ecaa6e6a527163045bb640dfba7b2c335aa
6
+ metadata.gz: cfca483374037e8bc4fdd70dd0c321c3b5bf37ac877a905acae8d93befd9c9ecdf8f537357852bbb095efbb247db9408d4cad78893fb245c0c0c8278b02463f4
7
+ data.tar.gz: 39f34eda4df97c673f2f482290642a5ec186d2b0017f95f4edef28a8adef055db8f1e47504c849556b0f6ab0167326ea5fc29009279442705840afb490838bee
@@ -40,6 +40,7 @@ Style/IfUnlessModifier: {Enabled: false}
40
40
  # Consistent to other definitions.
41
41
  Style/EmptyMethod: {EnforcedStyle: expanded}
42
42
 
43
+ Style/Lambda: {EnforcedStyle: literal}
43
44
  Style/ModuleFunction: {Enabled: false}
44
45
  Style/NestedParenthesizedCalls: {Enabled: false}
45
46
  Style/SignalException: {EnforcedStyle: only_raise}
@@ -1,5 +1,13 @@
1
1
  # Unreleased
2
2
 
3
+ # 0.15.0
4
+
5
+ - Add `:path` option to `telegram_webhook` route helper.
6
+ - __Breaking change!__ Default route is generated using hashed bot token.
7
+ Please reconfigure webhook after update (`rake telegram:bot:set_webhook`).
8
+ - Update to Bot API 5.0, add rake tasks for `deleteWebhook`, `close` & `logOut`.
9
+ - Add `server` option for client to support local bot API servers.
10
+
3
11
  # 0.14.4
4
12
 
5
13
  - Update to Bot API 4.7
data/README.md CHANGED
@@ -4,8 +4,6 @@
4
4
  [![Code Climate](https://codeclimate.com/github/telegram-bot-rb/telegram-bot/badges/gpa.svg)](https://codeclimate.com/github/telegram-bot-rb/telegram-bot)
5
5
  [![Build Status](https://travis-ci.org/telegram-bot-rb/telegram-bot.svg)](https://travis-ci.org/telegram-bot-rb/telegram-bot)
6
6
 
7
- __Breaking changes in v0.14!__ See [upgrade guide](https://github.com/telegram-bot-rb/telegram-bot/wiki/Upgrading-to-0.14).
8
-
9
7
  Tools for developing Telegram bots. Best used with Rails, but can be used in
10
8
  [standalone app](https://github.com/telegram-bot-rb/telegram-bot/wiki/Not-rails-application).
11
9
  Supposed to be used in webhook-mode in production, and poller-mode
@@ -65,7 +63,11 @@ which is used for `Telegram.bot`.
65
63
  ```ruby
66
64
  Telegram.bots_config = {
67
65
  default: DEFAULT_BOT_TOKEN,
68
- chat: {token: CHAT_BOT_TOKEN, username: 'chatbot'},
66
+ chat: {
67
+ token: CHAT_BOT_TOKEN,
68
+ username: 'ChatBot', # to support commands with mentions (/help@ChatBot)
69
+ server: 'http://local.bot.api.server', # for Local Bot API Server
70
+ },
69
71
  }
70
72
 
71
73
  Telegram.bot.get_updates
@@ -87,6 +89,7 @@ development:
87
89
  bot:
88
90
  token: TOKEN
89
91
  username: SomeBot
92
+ server: http://local.bot.api.server
90
93
 
91
94
  # For multiple bots in single app use hash of `internal_bot_id => settings`
92
95
  bots:
@@ -366,7 +369,7 @@ end
366
369
  ### Routes in Rails app
367
370
 
368
371
  There is `telegram_webhook` helper for rails app to define routes for webhooks.
369
- It defines routes at `telegram/#{bot.token}` and connects bots with controller.
372
+ It defines routes at `telegram/#{hash_of(bot.token)}` and connects bots with controller.
370
373
 
371
374
  ```ruby
372
375
  # Most off apps would require
@@ -25,7 +25,7 @@ method_list = headers.
25
25
  map { |g| g.reject { |x| x.match?(NOT_METHOD_REGEXP) } }.
26
26
  reject(&:empty?)
27
27
 
28
- api_version = doc.text.match(/^(Bot API ([\d\.]+))\.?$/)
28
+ api_version = doc.text.match(/^(?:Introducing )?(Bot API ([\d\.]+))\.?$/)
29
29
 
30
30
  result = ['# Generated with bin/fetch-telegram-methods']
31
31
  result << "# #{api_version[1]}" if api_version
@@ -14,15 +14,22 @@ namespace :telegram do
14
14
 
15
15
  desc 'Set webhook urls for all bots'
16
16
  task set_webhook: :environment do
17
- routes = Rails.application.routes.url_helpers
18
- cert_file = ENV['CERT']
19
- cert = File.open(cert_file) if cert_file
20
- Telegram.bots.each do |key, bot|
21
- route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
22
- url = routes.send("#{route_name}_url")
23
- puts "Setting webhook for #{key}..."
24
- bot.async(false) { bot.set_webhook(url: url, certificate: cert) }
25
- end
17
+ Telegram::Bot::Tasks.set_webhook
18
+ end
19
+
20
+ desc 'Delete webhooks for all or specific BOT'
21
+ task :delete_webhook do
22
+ Telegram::Bot::Tasks.delete_webhook
23
+ end
24
+
25
+ desc 'Perform logOut command for all or specific BOT'
26
+ task :log_out do
27
+ Telegram::Bot::Tasks.log_out
28
+ end
29
+
30
+ desc 'Perform `close` command for all or specific BOT'
31
+ task :close do
32
+ Telegram::Bot::Tasks.close
26
33
  end
27
34
  end
28
35
  end
@@ -14,10 +14,10 @@ module Telegram
14
14
 
15
15
  module_function
16
16
 
17
- def deprecation_0_15
17
+ def deprecation_0_16
18
18
  @deprecation ||= begin
19
19
  require 'active_support/deprecation'
20
- ActiveSupport::Deprecation.new('0.15', 'Telegram::Bot')
20
+ ActiveSupport::Deprecation.new('0.16', 'Telegram::Bot')
21
21
  end
22
22
  end
23
23
 
@@ -25,9 +25,9 @@ module Telegram
25
25
  autoload :Client, 'telegram/bot/client'
26
26
  autoload :ClientStub, 'telegram/bot/client_stub'
27
27
  autoload :DebugClient, 'telegram/bot/debug_client'
28
- autoload :Initializers, 'telegram/bot/initializers'
29
28
  autoload :Middleware, 'telegram/bot/middleware'
30
29
  autoload :RSpec, 'telegram/bot/rspec'
30
+ autoload :Tasks, 'telegram/bot/tasks'
31
31
  autoload :UpdatesController, 'telegram/bot/updates_controller'
32
32
  autoload :UpdatesPoller, 'telegram/bot/updates_poller'
33
33
  end
@@ -1,13 +1,14 @@
1
+ require 'active_support/core_ext/hash/keys'
1
2
  require 'json'
2
3
  require 'httpclient'
3
4
 
4
5
  module Telegram
5
6
  module Bot
6
7
  class Client
7
- URL_TEMPLATE = 'https://api.telegram.org/bot%<token>s/'.freeze
8
+ SERVER = 'https://api.telegram.org'.freeze
9
+ URL_TEMPLATE = '%<server>s/bot%<token>s/'.freeze
8
10
 
9
11
  autoload :TypedResponse, 'telegram/bot/client/typed_response'
10
- extend Initializers
11
12
  prepend Async
12
13
  include DebugClient
13
14
 
@@ -15,6 +16,16 @@ module Telegram
15
16
  include ApiHelper
16
17
 
17
18
  class << self
19
+ # Accepts different options to initialize bot.
20
+ def wrap(input, **options)
21
+ case input
22
+ when Symbol then by_id(input) or raise "#{name} #{input.inspect} not configured"
23
+ when self then input
24
+ when Hash then new(**input.symbolize_keys, **options)
25
+ else new(input, **options)
26
+ end
27
+ end
28
+
18
29
  def by_id(id)
19
30
  Telegram.bots[id]
20
31
  end
@@ -51,11 +62,11 @@ module Telegram
51
62
 
52
63
  attr_reader :client, :token, :username, :base_uri
53
64
 
54
- def initialize(token = nil, username = nil, **options)
65
+ def initialize(token = nil, username = nil, server: SERVER, **options)
55
66
  @client = HTTPClient.new
56
67
  @token = token || options[:token]
57
68
  @username = username || options[:username]
58
- @base_uri = format(URL_TEMPLATE, token: self.token)
69
+ @base_uri = format(URL_TEMPLATE, server: server, token: self.token)
59
70
  end
60
71
 
61
72
  def request(action, body = {})
@@ -1,5 +1,5 @@
1
1
  # Generated with bin/fetch-telegram-methods
2
- # Bot API 4.7
2
+ # Bot API 5.0
3
3
 
4
4
  getUpdates
5
5
  setWebhook
@@ -7,8 +7,11 @@ deleteWebhook
7
7
  getWebhookInfo
8
8
 
9
9
  getMe
10
+ logOut
11
+ close
10
12
  sendMessage
11
13
  forwardMessage
14
+ copyMessage
12
15
  sendPhoto
13
16
  sendAudio
14
17
  sendDocument
@@ -40,6 +43,7 @@ setChatTitle
40
43
  setChatDescription
41
44
  pinChatMessage
42
45
  unpinChatMessage
46
+ unpinAllChatMessages
43
47
  leaveChat
44
48
  getChat
45
49
  getChatAdministrators
@@ -1,3 +1,6 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
1
4
  require 'telegram/bot'
2
5
  require 'active_support/core_ext/array/wrap'
3
6
 
@@ -17,10 +20,8 @@ module Telegram
17
20
  end || 'telegram_webhook'
18
21
  end
19
22
 
20
- # Replaces colon with underscore so rails don't treat it as
21
- # route parameter.
22
- def escape_token(token)
23
- token && token.tr(':', '_')
23
+ def token_hash(token)
24
+ Base64.urlsafe_encode64(OpenSSL::Digest::SHA1.digest(token), padding: false)
24
25
  end
25
26
  end
26
27
 
@@ -34,14 +35,17 @@ module Telegram
34
35
  #
35
36
  # # pass additional options
36
37
  # telegram_webhook TelegramController, :default, as: :custom_route_name
37
- def telegram_webhook(controller, bot = :default, **options)
38
+ #
39
+ # # Default path is generated using hashed bot token. Override it using:
40
+ # telegram_webhook TelegramController, :default, path: 'top/secret'
41
+ def telegram_webhook(controller, bot = :default, path: nil, **options)
38
42
  bot = Client.wrap(bot)
39
43
  params = {
40
44
  to: Middleware.new(bot, controller),
41
45
  as: RoutesHelper.route_name_for_bot(bot),
42
46
  format: false,
43
47
  }.merge!(options)
44
- post("telegram/#{RoutesHelper.escape_token bot.token}", params)
48
+ post(path || "telegram/#{bot.token && RoutesHelper.token_hash(bot.token)}", params)
45
49
  UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode?
46
50
  end
47
51
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telegram
4
+ module Bot
5
+ module Tasks
6
+ extend self
7
+
8
+ def set_webhook
9
+ routes = Rails.application.routes.url_helpers
10
+ cert_file = ENV['CERT']
11
+ cert = File.open(cert_file) if cert_file
12
+ each_bot do |key, bot|
13
+ route_name = RoutesHelper.route_name_for_bot(bot)
14
+ url = routes.send("#{route_name}_url")
15
+ say("Setting webhook for #{key}...")
16
+ bot.set_webhook(
17
+ url: url,
18
+ certificate: cert,
19
+ ip_address: ENV['IP_ADDRESS'],
20
+ drop_pending_updates: drop_pending_updates,
21
+ )
22
+ end
23
+ end
24
+
25
+ def delete_webhook
26
+ each_bot do |key, bot|
27
+ say("Deleting webhook for #{key}...")
28
+ bot.delete_webhook(drop_pending_updates: drop_pending_updates)
29
+ end
30
+ end
31
+
32
+ def log_out
33
+ each_bot do |key, bot|
34
+ say("Logging out #{key}...")
35
+ bot.log_out
36
+ end
37
+ end
38
+
39
+ def close
40
+ each_bot do |key, bot|
41
+ say("Closing #{key}...")
42
+ bot.close
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def say(text)
49
+ puts(text) unless Rails.env.test? # rubocop:disable Rails/Output
50
+ end
51
+
52
+ def each_bot(&block)
53
+ id = ENV['BOT'].try!(:to_sym)
54
+ bots = id ? {id => Client.by_id(id)} : Telegram.bots
55
+ bots.each { |key, bot| bot.async(false) { block[key, bot] } }
56
+ end
57
+
58
+ def drop_pending_updates
59
+ ENV['DROP_PENDING_UPDATES'].try!(:downcase) == 'true'
60
+ end
61
+ end
62
+ end
63
+ end
@@ -50,7 +50,7 @@ module Telegram
50
50
  ensure
51
51
  @running = false
52
52
  end
53
- log { 'Stoped polling bot updates.' }
53
+ log { 'Stopped polling bot updates.' }
54
54
  end
55
55
 
56
56
  def run
@@ -1,6 +1,6 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.14.4'.freeze
3
+ VERSION = '0.15.0'.freeze
4
4
 
5
5
  def self.gem_version
6
6
  Gem::Version.new VERSION
@@ -17,9 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
18
  spec.require_paths = ['lib']
19
19
 
20
- spec.post_install_message = 'Breaking changes in v0.14! ' \
21
- 'See upgrade guide at https://github.com/telegram-bot-rb/telegram-bot/wiki/Upgrading-to-0.14'
22
-
23
20
  spec.required_ruby_version = '~> 2.0'
24
21
 
25
22
  spec.add_dependency 'actionpack', '>= 4.0', '< 6.1'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.4
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Melentiev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-15 00:00:00.000000000 Z
11
+ date: 2020-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -131,19 +131,18 @@ files:
131
131
  - lib/telegram/bot/client_stub.rb
132
132
  - lib/telegram/bot/config_methods.rb
133
133
  - lib/telegram/bot/debug_client.rb
134
- - lib/telegram/bot/initializers.rb
135
134
  - lib/telegram/bot/middleware.rb
136
135
  - lib/telegram/bot/railtie.rb
137
136
  - lib/telegram/bot/routes_helper.rb
138
137
  - lib/telegram/bot/rspec.rb
139
138
  - lib/telegram/bot/rspec/callback_query_helpers.rb
140
139
  - lib/telegram/bot/rspec/client_matchers.rb
141
- - lib/telegram/bot/rspec/integration.rb
142
140
  - lib/telegram/bot/rspec/integration/poller.rb
143
141
  - lib/telegram/bot/rspec/integration/rack.rb
144
142
  - lib/telegram/bot/rspec/integration/rails.rb
145
143
  - lib/telegram/bot/rspec/integration/shared.rb
146
144
  - lib/telegram/bot/rspec/message_helpers.rb
145
+ - lib/telegram/bot/tasks.rb
147
146
  - lib/telegram/bot/updates_controller.rb
148
147
  - lib/telegram/bot/updates_controller/callback_query_context.rb
149
148
  - lib/telegram/bot/updates_controller/commands.rb
@@ -164,7 +163,7 @@ homepage: https://github.com/telegram-bot-rb/telegram-bot
164
163
  licenses:
165
164
  - MIT
166
165
  metadata: {}
167
- post_install_message: Breaking changes in v0.14! See upgrade guide at https://github.com/telegram-bot-rb/telegram-bot/wiki/Upgrading-to-0.14
166
+ post_install_message:
168
167
  rdoc_options: []
169
168
  require_paths:
170
169
  - lib
@@ -179,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
178
  - !ruby/object:Gem::Version
180
179
  version: '0'
181
180
  requirements: []
182
- rubygems_version: 3.0.3
181
+ rubygems_version: 3.1.4
183
182
  signing_key:
184
183
  specification_version: 4
185
184
  summary: Library for building Telegram Bots with Rails integration
@@ -1,21 +0,0 @@
1
- require 'active_support/core_ext/hash/keys'
2
-
3
- module Telegram
4
- module Bot
5
- module Initializers
6
- # Accepts different options to initialize bot.
7
- def wrap(input, **options)
8
- case input
9
- when Symbol then by_id(input) or raise "#{name} #{input.inspect} not configured"
10
- when self then input
11
- when Hash then new(**input.symbolize_keys, **options)
12
- else new(input, **options)
13
- end
14
- end
15
-
16
- def by_id(_id)
17
- raise 'Not implemented'
18
- end
19
- end
20
- end
21
- end
@@ -1,10 +0,0 @@
1
- require 'telegram/bot'
2
- Telegram::Bot.deprecation_0_15.warn(
3
- "`require 'telegram/bot/rspec/integration'` is deprecated in favor of " \
4
- "`require 'telegram/bot/rspec/integration/rails'`"
5
- )
6
- require 'telegram/bot/rspec/integration/rails'
7
-
8
- Telegram::Bot::RSpec.with_include_context do |config|
9
- config.include_context 'telegram/bot/integration/rails', telegram_bot: true
10
- end