telegram-bot 0.14.1 → 0.15.1

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: e6d77239250dfe0b0929e91f8bb625282ba34d34e242a5a138e37d51b14281e8
4
- data.tar.gz: e2471ee47e80f2b648d078cad288cad4b5fb15417d7b8dd37ed59cb002b555ca
3
+ metadata.gz: 113ad4cfd9374055cf8da30c1628d50929f4142177755fbeae9bff8d96381996
4
+ data.tar.gz: b161be1ea1c8509998b1421b86c96db9e8a1b6c10b6630f0bc9eea6adb806310
5
5
  SHA512:
6
- metadata.gz: 1b930c85ae8b1c4fd8dff076243466c4da6e22794828f4a7f4827a677104a1ed4a756abc301f79cbb49333be02442c10c0a53085a35b5f5eddf70df34aff3ae5
7
- data.tar.gz: c96a953d8d7bf36ecb421a8e985383c9f91c78e540c070d46cfe579c625ad981e81820e034908aafde27d4c2151c513f391c5863d1d5d1617d714325e55f955f
6
+ metadata.gz: 72d227b63dcdfb7d95fcafbb5fa564688a62433916ddad105b7a0784a65a053ddd379aac943645b1ab592ea02b3023577d15a187cbee3d3216e9227bae027405
7
+ data.tar.gz: a7449ef3e57885b7e7aa1be9ce32a2c05907401619727fe4a5acada4ace148d0bb0c7590837c8e62531daa8e1bdd2930b172ca6c566100cab6eedb4df3eac896
@@ -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,34 +1,25 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  rvm:
4
+ - 2.6
4
5
  - 2.5
5
- - 2.4
6
6
  gemfile:
7
+ - gemfiles/rails_61.gemfile
8
+ - gemfiles/rails_60.gemfile
7
9
  - gemfiles/rails_52.gemfile
8
- - gemfiles/rails_51.gemfile
9
10
  matrix:
10
11
  include:
11
12
  # old rails
13
+ - rvm: 2.5
14
+ gemfile: gemfiles/rails_51.gemfile
12
15
  - rvm: 2.5
13
16
  gemfile: gemfiles/rails_50.gemfile
14
17
  - rvm: 2.5
15
18
  gemfile: gemfiles/rails_42.gemfile
16
19
  # old rubies
20
+ - rvm: 2.4
21
+ gemfile: gemfiles/rails_52.gemfile
17
22
  - rvm: 2.3
18
23
  gemfile: gemfiles/rails_52.gemfile
19
- # future rails
20
- - rvm: 2.5
21
- gemfile: gemfiles/rails_60.gemfile
22
- # Allow rails 6 to fail while master branch is used
23
- allow_failures:
24
- - rvm: 2.5
25
- gemfile: gemfiles/rails_60.gemfile
26
24
  notifications:
27
25
  email: false
28
-
29
- # for 2.5.0 until 2.5.1 is released:
30
- # https://github.com/travis-ci/travis-ci/issues/8978
31
- # https://github.com/travis-ci/travis-ci/issues/8969#issuecomment-354135622
32
- before_install:
33
- - gem update --system
34
- - gem install bundler
data/Appraisals CHANGED
@@ -1,26 +1,29 @@
1
+ appraise 'rails-61' do
2
+ gem 'actionpack', '~> 6.1.0'
3
+ gem 'railties', '~> 6.1.0'
4
+ end
5
+
1
6
  appraise 'rails-60' do
2
- git 'https://github.com/rails/rails.git' do
3
- gem 'actionpack', '~> 6.0.0.alpha'
4
- gem 'railties', '~> 6.0.0.alpha'
5
- end
7
+ gem 'actionpack', '~> 6.0.0'
8
+ gem 'railties', '~> 6.0.0'
6
9
  end
7
10
 
8
11
  appraise 'rails-52' do
9
- gem 'actionpack', '~> 5.2.0.beta2'
10
- gem 'railties', '~> 5.2.0.beta2'
12
+ gem 'actionpack', '~> 5.2.0'
13
+ gem 'railties', '~> 5.2.0'
11
14
  end
12
15
 
13
16
  appraise 'rails-51' do
14
- gem 'actionpack', '~> 5.1'
15
- gem 'railties', '~> 5.1'
17
+ gem 'actionpack', '~> 5.1.0'
18
+ gem 'railties', '~> 5.1.0'
16
19
  end
17
20
 
18
21
  appraise 'rails-50' do
19
- gem 'actionpack', '~> 5.0'
20
- gem 'railties', '~> 5.0'
22
+ gem 'actionpack', '~> 5.0.0'
23
+ gem 'railties', '~> 5.0.0'
21
24
  end
22
25
 
23
26
  appraise 'rails-42' do
24
- gem 'actionpack', '~> 4.2'
25
- gem 'railties', '~> 4.2'
27
+ gem 'actionpack', '~> 4.2.0'
28
+ gem 'railties', '~> 4.2.0'
26
29
  end
@@ -1,8 +1,37 @@
1
1
  # Unreleased
2
2
 
3
+ # 0.15.1
4
+
5
+ - Rails 6.1 support.
6
+
7
+ # 0.15.0
8
+
9
+ - Add `:path` option to `telegram_webhook` route helper.
10
+ - __Breaking change!__ Default route is generated using hashed bot token.
11
+ Please reconfigure webhook after update (`rake telegram:bot:set_webhook`).
12
+ - Update to Bot API 5.0, add rake tasks for `deleteWebhook`, `close` & `logOut`.
13
+ - Add `server` option for client to support local bot API servers.
14
+
15
+ # 0.14.4
16
+
17
+ - Update to Bot API 4.7
18
+
19
+ # 0.14.3
20
+
21
+ - Rails 6.0 support.
22
+ - Update to Bot API 4.4.
23
+
24
+ # 0.14.2
25
+
26
+ - Add reply helpers: `answer_pre_checkout_query`, `answer_shipping_query`.
27
+ - Update to Bot API 4.3.
28
+
29
+ # 0.14.1
30
+
3
31
  - Read config from secrets when credentials don't provide it in rails >= 5.2.
4
32
  - Remove botan.io support. It's already shut down, so it should not be a braking change.
5
33
  https://github.com/botanio/sdk#this-service-will-be-shut-down-on-25th-may-2018
34
+ - Update to Bot API 4.1.
6
35
 
7
36
  # 0.14.0
8
37
 
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ group :development do
8
8
  gem 'pry-byebug', '~> 3.2.0'
9
9
  gem 'sdoc', '~> 0.4.1'
10
10
 
11
- gem 'telegram-bot-types', '~> 0.6.0'
11
+ gem 'telegram-bot-types', '~> 0.6.2'
12
12
 
13
13
  gem 'rspec', '~> 3.5.0'
14
14
  gem 'rspec-its', '~> 1.1.0'
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:
@@ -100,10 +103,12 @@ development:
100
103
 
101
104
  For Rails >= 5.2 `Telegram::Bot` searches for config first in credentials and then in secrets.
102
105
  To use credentials as config store, add telegram section to credentials instead of secrets using
103
- `rails credentials:edit`. In this case be aware of that [Rails may not load
106
+ `rails credentials:edit`. In this case be aware of that [Rails < 6.0 may not load
104
107
  credentials in dev environment by default](https://github.com/telegram-bot-rb/telegram-bot/issues/74#issuecomment-384205609).
108
+ In Rails >= 6.0 run `rails credentials:edit --environment development` to configure bot
109
+ in each environment.
105
110
 
106
- I suggest not using Rails 5.2 credentials because it can lead to leakage of sesitive data
111
+ I suggest not using Rails 5.2 credentials because it can lead to leakage of sensitive data
107
112
  and it's more difficult to use in multiple environments. See
108
113
  [secure_credentials](https://github.com/printercu/secure_credentials) gem for better option.
109
114
 
@@ -229,6 +234,8 @@ def reply_with(type, params); end
229
234
  def answer_inline_query(results, params = {}); end
230
235
  def answer_callback_query(text, params = {}); end
231
236
  def edit_message(type, params = {}); end
237
+ def answer_pre_checkout_query(ok, params = {}); end
238
+ def answer_shipping_query(ok, params = {}); end
232
239
  ```
233
240
 
234
241
  #### Optional typecasting
@@ -362,7 +369,7 @@ end
362
369
  ### Routes in Rails app
363
370
 
364
371
  There is `telegram_webhook` helper for rails app to define routes for webhooks.
365
- 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.
366
373
 
367
374
  ```ruby
368
375
  # Most off apps would require
@@ -471,10 +478,11 @@ RSpec.describe TelegramWebhooksController, telegram_bot: :rails do
471
478
  # dispatch_command(cmd, *args)
472
479
 
473
480
  # Available matchers can be found in Telegram::Bot::RSpec::ClientMatchers.
474
- it 'shows usage of basic matchers'
475
- # The most basic one is #make_telegram_request(bot, endpoint, params_matcher)
476
- expect { dispatch_command(:start) }.
477
- to make_telegram_request(bot, :sendMessage, hash_including(text: 'msg text'))
481
+ it 'shows usage of basic matchers' do
482
+ # The most basic one is #make_telegram_request(bot, action).
483
+ # It works similar to `receive` matcher and supports chaining `.with(args).exactly(n).times`.
484
+ expect { dispatch_command(:start) }.to make_telegram_request(bot, :sendMessage).
485
+ with(hash_including(text: 'msg text'))
478
486
 
479
487
  # There are some shortcuts for dispatching basic updates and testing responses.
480
488
  expect { dispatch_message('Hi') }.to send_telegram_message(bot, /msg regexp/, some: :option)
@@ -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
@@ -2,15 +2,15 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "actionpack", "~> 4.2"
6
- gem "railties", "~> 4.2"
5
+ gem "actionpack", "~> 4.2.0"
6
+ gem "railties", "~> 4.2.0"
7
7
 
8
8
  group :development do
9
9
  gem "appraisal", "~> 2.2"
10
10
  gem "pry", "~> 0.10.1"
11
11
  gem "pry-byebug", "~> 3.2.0"
12
12
  gem "sdoc", "~> 0.4.1"
13
- gem "telegram-bot-types", "~> 0.6.0"
13
+ gem "telegram-bot-types", "~> 0.6.2"
14
14
  gem "rspec", "~> 3.5.0"
15
15
  gem "rspec-its", "~> 1.1.0"
16
16
  gem "rspec-rails", "~> 3.5.0"
@@ -2,15 +2,15 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "actionpack", "~> 5.0"
6
- gem "railties", "~> 5.0"
5
+ gem "actionpack", "~> 5.0.0"
6
+ gem "railties", "~> 5.0.0"
7
7
 
8
8
  group :development do
9
9
  gem "appraisal", "~> 2.2"
10
10
  gem "pry", "~> 0.10.1"
11
11
  gem "pry-byebug", "~> 3.2.0"
12
12
  gem "sdoc", "~> 0.4.1"
13
- gem "telegram-bot-types", "~> 0.6.0"
13
+ gem "telegram-bot-types", "~> 0.6.2"
14
14
  gem "rspec", "~> 3.5.0"
15
15
  gem "rspec-its", "~> 1.1.0"
16
16
  gem "rspec-rails", "~> 3.5.0"
@@ -2,15 +2,15 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "actionpack", "~> 5.1"
6
- gem "railties", "~> 5.1"
5
+ gem "actionpack", "~> 5.1.0"
6
+ gem "railties", "~> 5.1.0"
7
7
 
8
8
  group :development do
9
9
  gem "appraisal", "~> 2.2"
10
10
  gem "pry", "~> 0.10.1"
11
11
  gem "pry-byebug", "~> 3.2.0"
12
12
  gem "sdoc", "~> 0.4.1"
13
- gem "telegram-bot-types", "~> 0.6.0"
13
+ gem "telegram-bot-types", "~> 0.6.2"
14
14
  gem "rspec", "~> 3.5.0"
15
15
  gem "rspec-its", "~> 1.1.0"
16
16
  gem "rspec-rails", "~> 3.5.0"
@@ -2,15 +2,15 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "actionpack", "~> 5.2.0.beta2"
6
- gem "railties", "~> 5.2.0.beta2"
5
+ gem "actionpack", "~> 5.2.0"
6
+ gem "railties", "~> 5.2.0"
7
7
 
8
8
  group :development do
9
9
  gem "appraisal", "~> 2.2"
10
10
  gem "pry", "~> 0.10.1"
11
11
  gem "pry-byebug", "~> 3.2.0"
12
12
  gem "sdoc", "~> 0.4.1"
13
- gem "telegram-bot-types", "~> 0.6.0"
13
+ gem "telegram-bot-types", "~> 0.6.2"
14
14
  gem "rspec", "~> 3.5.0"
15
15
  gem "rspec-its", "~> 1.1.0"
16
16
  gem "rspec-rails", "~> 3.5.0"
@@ -2,17 +2,15 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- git "https://github.com/rails/rails.git" do
6
- gem "actionpack", "~> 6.0.0.alpha"
7
- gem "railties", "~> 6.0.0.alpha"
8
- end
5
+ gem "actionpack", "~> 6.0.0"
6
+ gem "railties", "~> 6.0.0"
9
7
 
10
8
  group :development do
11
9
  gem "appraisal", "~> 2.2"
12
10
  gem "pry", "~> 0.10.1"
13
11
  gem "pry-byebug", "~> 3.2.0"
14
12
  gem "sdoc", "~> 0.4.1"
15
- gem "telegram-bot-types", "~> 0.6.0"
13
+ gem "telegram-bot-types", "~> 0.6.2"
16
14
  gem "rspec", "~> 3.5.0"
17
15
  gem "rspec-its", "~> 1.1.0"
18
16
  gem "rspec-rails", "~> 3.5.0"
@@ -0,0 +1,21 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "actionpack", "~> 6.1.0"
6
+ gem "railties", "~> 6.1.0"
7
+
8
+ group :development do
9
+ gem "appraisal", "~> 2.2"
10
+ gem "pry", "~> 0.10.1"
11
+ gem "pry-byebug", "~> 3.2.0"
12
+ gem "sdoc", "~> 0.4.1"
13
+ gem "telegram-bot-types", "~> 0.6.2"
14
+ gem "rspec", "~> 3.5.0"
15
+ gem "rspec-its", "~> 1.1.0"
16
+ gem "rspec-rails", "~> 3.5.0"
17
+ gem "rubocop", "~> 0.52.1"
18
+ gem "coveralls", "~> 0.8.2", require: false
19
+ end
20
+
21
+ gemspec path: "../"
@@ -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
@@ -16,8 +16,8 @@ module Telegram
16
16
  # accessible via `Teletgram.bots[id]` in job worker. Or just use
17
17
  # `Telegram.bots_config=` for configuration.
18
18
  #
19
- # Being in async mode `#request` enqueues job instead to perform
20
- # http request instead of performing it directly.
19
+ # Being in async mode `#request` enqueues job to perform
20
+ # http request instead of performing it immediately.
21
21
  # Async behavior is controlled with `#async=` writer
22
22
  # and can be enabled/disabled for the block with `#async`:
23
23
  #
@@ -29,7 +29,7 @@ module Telegram
29
29
  # while `#async(val, &block)` is thread-safe.
30
30
  #
31
31
  # It can be set with custom job class or classname. By default it defines
32
- # job classes for every client class, inherited from ApplicationRecord, which
32
+ # job classes inherited from ApplicationJob, which
33
33
  # can be accessed via `.default_async_job`. You can integrate it with any
34
34
  # other job provider by defining a class with `.perform_later(bot_id, *args)`
35
35
  # method. See Async::Job for implemetation.
@@ -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.1
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
@@ -22,6 +25,8 @@ editMessageLiveLocation
22
25
  stopMessageLiveLocation
23
26
  sendVenue
24
27
  sendContact
28
+ sendPoll
29
+ sendDice
25
30
  sendChatAction
26
31
  getUserProfilePhotos
27
32
  getFile
@@ -29,6 +34,8 @@ kickChatMember
29
34
  unbanChatMember
30
35
  restrictChatMember
31
36
  promoteChatMember
37
+ setChatAdministratorCustomTitle
38
+ setChatPermissions
32
39
  exportChatInviteLink
33
40
  setChatPhoto
34
41
  deleteChatPhoto
@@ -36,6 +43,7 @@ setChatTitle
36
43
  setChatDescription
37
44
  pinChatMessage
38
45
  unpinChatMessage
46
+ unpinAllChatMessages
39
47
  leaveChat
40
48
  getChat
41
49
  getChatAdministrators
@@ -44,11 +52,14 @@ getChatMember
44
52
  setChatStickerSet
45
53
  deleteChatStickerSet
46
54
  answerCallbackQuery
55
+ setMyCommands
56
+ getMyCommands
47
57
 
48
58
  editMessageText
49
59
  editMessageCaption
50
60
  editMessageMedia
51
61
  editMessageReplyMarkup
62
+ stopPoll
52
63
  deleteMessage
53
64
 
54
65
  sendSticker
@@ -58,6 +69,7 @@ createNewStickerSet
58
69
  addStickerToSet
59
70
  setStickerPositionInSet
60
71
  deleteStickerFromSet
72
+ setStickerSetThumb
61
73
 
62
74
  answerInlineQuery
63
75
 
@@ -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
@@ -7,13 +7,13 @@ module Telegram
7
7
  module Bot
8
8
  # Base class to create update processors. With callbacks, session and helpers.
9
9
  #
10
- # Define public methods for each command and they will be called when
11
- # update has this command. Message is automatically parsed and
12
- # words are passed as method arguments. Be sure to use default values and
10
+ # Public methods ending with `!` handle messages with commands.
11
+ # Message text is automatically parsed into method arguments.
12
+ # Be sure to use default values and
13
13
  # splat arguments in every action method to not get errors, when user
14
14
  # sends command without necessary args / with extra args.
15
15
  #
16
- # def start(token = nil, *)
16
+ # def start!(token = nil, *)
17
17
  # if token
18
18
  # # ...
19
19
  # else
@@ -21,25 +21,21 @@ module Telegram
21
21
  # end
22
22
  # end
23
23
  #
24
- # def help(*)
24
+ # def help!(*)
25
25
  # respond_with :message, text:
26
26
  # end
27
27
  #
28
28
  # To process plain text messages (without commands) or other updates just
29
- # define public method with name of payload type. They will receive payload
30
- # as an argument.
29
+ # define public method with name of payload type.
30
+ # By default they receive payload as an argument, but some of them are called
31
+ # with more usefuk args:
31
32
  #
32
33
  # def message(message)
33
34
  # respond_with :message, text: "Echo: #{message['text']}"
34
35
  # end
35
36
  #
36
- # def inline_query(query)
37
- # answer_inline_query results_for_query(query), is_personal: true
38
- # end
39
- #
40
- # # To process conflicting commands (`/message args`) just use `on_` prefix:
41
- # def on_message(*args)
42
- # # ...
37
+ # def inline_query(query, offset)
38
+ # answer_inline_query results_for_query(query, offset), is_personal: true
43
39
  # end
44
40
  #
45
41
  # To process update run:
@@ -101,6 +97,8 @@ module Telegram
101
97
  callback_query
102
98
  shipping_query
103
99
  pre_checkout_query
100
+ poll
101
+ poll_answer
104
102
  ].freeze
105
103
 
106
104
  class << self
@@ -152,7 +150,7 @@ module Telegram
152
150
  # Accessor to `'from'` field of payload. Can be overriden with `from` option
153
151
  # for #initialize.
154
152
  def from
155
- @_from ||= payload && payload['from']
153
+ @_from ||= payload.is_a?(Hash) ? payload['from'] : payload.try(:from)
156
154
  end
157
155
 
158
156
  # Processes current update.
@@ -210,6 +208,10 @@ module Telegram
210
208
  [payload_type, [payload['data']]]
211
209
  end
212
210
 
211
+ def action_for_poll_answer
212
+ [payload_type, [payload['poll_id'], payload['option_ids']]]
213
+ end
214
+
213
215
  # Silently ignore unsupported messages to not fail when user crafts
214
216
  # an update with usupported command, callback query context, etc.
215
217
  def action_missing(action, *_args)
@@ -49,7 +49,7 @@ module Telegram
49
49
  private
50
50
 
51
51
  # A hook invoked every time a before callback is halted.
52
- def halted_callback_hook(filter)
52
+ def halted_callback_hook(filter, *)
53
53
  Instrumentation.instrument(:halted_callback, filter: filter)
54
54
  end
55
55
 
@@ -21,7 +21,7 @@ module Telegram
21
21
  respond_with(type, params)
22
22
  end
23
23
 
24
- # Same as reply_with, but for inline queries.
24
+ # Same as respond_with, but for inline queries.
25
25
  def answer_inline_query(results, params = {})
26
26
  params = params.merge(
27
27
  inline_query_id: payload['id'],
@@ -30,7 +30,7 @@ module Telegram
30
30
  bot.answer_inline_query(params)
31
31
  end
32
32
 
33
- # Same as reply_with, but for callback queries.
33
+ # Same as respond_with, but for callback queries.
34
34
  def answer_callback_query(text, params = {})
35
35
  params = params.merge(
36
36
  callback_query_id: payload['id'],
@@ -39,6 +39,23 @@ module Telegram
39
39
  bot.answer_callback_query(params)
40
40
  end
41
41
 
42
+ # Same as respond_with, but for pre checkout queries.
43
+ def answer_pre_checkout_query(ok, params = {})
44
+ params = params.merge(
45
+ pre_checkout_query_id: payload['id'],
46
+ ok: ok,
47
+ )
48
+ bot.answer_pre_checkout_query(params)
49
+ end
50
+
51
+ def answer_shipping_query(ok, params = {})
52
+ params = params.merge(
53
+ shipping_query_id: payload['id'],
54
+ ok: ok,
55
+ )
56
+ bot.answer_shipping_query(params)
57
+ end
58
+
42
59
  # Edit message from callback query.
43
60
  def edit_message(type, params = {})
44
61
  params =
@@ -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.1'.freeze
3
+ VERSION = '0.15.1'.freeze
4
4
 
5
5
  def self.gem_version
6
6
  Gem::Version.new VERSION
@@ -17,15 +17,12 @@ 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
- spec.add_dependency 'actionpack', '>= 4.0', '< 6.0'
26
- spec.add_dependency 'activesupport', '>= 4.0', '< 6.0'
22
+ spec.add_dependency 'actionpack', '>= 4.0', '< 6.2'
23
+ spec.add_dependency 'activesupport', '>= 4.0', '< 6.2'
27
24
  spec.add_dependency 'httpclient', '~> 2.7'
28
25
 
29
- spec.add_development_dependency 'bundler', '~> 1.16'
30
- spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'bundler', '> 1.16'
27
+ spec.add_development_dependency 'rake', '~> 13.0'
31
28
  end
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.1
4
+ version: 0.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Melentiev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-03 00:00:00.000000000 Z
11
+ date: 2020-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '4.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.0'
22
+ version: '6.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '4.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.0'
32
+ version: '6.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: activesupport
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: '4.0'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '6.0'
42
+ version: '6.2'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,7 +49,7 @@ dependencies:
49
49
  version: '4.0'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '6.0'
52
+ version: '6.2'
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: httpclient
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -68,14 +68,14 @@ dependencies:
68
68
  name: bundler
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  requirements:
71
- - - "~>"
71
+ - - ">"
72
72
  - !ruby/object:Gem::Version
73
73
  version: '1.16'
74
74
  type: :development
75
75
  prerelease: false
76
76
  version_requirements: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - "~>"
78
+ - - ">"
79
79
  - !ruby/object:Gem::Version
80
80
  version: '1.16'
81
81
  - !ruby/object:Gem::Dependency
@@ -84,14 +84,14 @@ dependencies:
84
84
  requirements:
85
85
  - - "~>"
86
86
  - !ruby/object:Gem::Version
87
- version: '10.0'
87
+ version: '13.0'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
- version: '10.0'
94
+ version: '13.0'
95
95
  description:
96
96
  email:
97
97
  - melentievm@gmail.com
@@ -120,6 +120,7 @@ files:
120
120
  - gemfiles/rails_51.gemfile
121
121
  - gemfiles/rails_52.gemfile
122
122
  - gemfiles/rails_60.gemfile
123
+ - gemfiles/rails_61.gemfile
123
124
  - lib/tasks/telegram-bot.rake
124
125
  - lib/telegram/bot.rb
125
126
  - lib/telegram/bot/async.rb
@@ -130,19 +131,18 @@ files:
130
131
  - lib/telegram/bot/client_stub.rb
131
132
  - lib/telegram/bot/config_methods.rb
132
133
  - lib/telegram/bot/debug_client.rb
133
- - lib/telegram/bot/initializers.rb
134
134
  - lib/telegram/bot/middleware.rb
135
135
  - lib/telegram/bot/railtie.rb
136
136
  - lib/telegram/bot/routes_helper.rb
137
137
  - lib/telegram/bot/rspec.rb
138
138
  - lib/telegram/bot/rspec/callback_query_helpers.rb
139
139
  - lib/telegram/bot/rspec/client_matchers.rb
140
- - lib/telegram/bot/rspec/integration.rb
141
140
  - lib/telegram/bot/rspec/integration/poller.rb
142
141
  - lib/telegram/bot/rspec/integration/rack.rb
143
142
  - lib/telegram/bot/rspec/integration/rails.rb
144
143
  - lib/telegram/bot/rspec/integration/shared.rb
145
144
  - lib/telegram/bot/rspec/message_helpers.rb
145
+ - lib/telegram/bot/tasks.rb
146
146
  - lib/telegram/bot/updates_controller.rb
147
147
  - lib/telegram/bot/updates_controller/callback_query_context.rb
148
148
  - lib/telegram/bot/updates_controller/commands.rb
@@ -163,7 +163,7 @@ homepage: https://github.com/telegram-bot-rb/telegram-bot
163
163
  licenses:
164
164
  - MIT
165
165
  metadata: {}
166
- 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:
167
167
  rdoc_options: []
168
168
  require_paths:
169
169
  - lib
@@ -178,8 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  - !ruby/object:Gem::Version
179
179
  version: '0'
180
180
  requirements: []
181
- rubyforge_project:
182
- rubygems_version: 2.7.6
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