зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-04 18:25:51 +03:00
Сравнить коммиты
10 Коммитов
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
2eb2670ed2 | ||
|
|
6a5e642470 | ||
|
|
4ff5a58b98 | ||
|
|
ffb13321b4 | ||
|
|
3bdc4f5ae0 | ||
|
|
5603016e4a | ||
|
|
fa6b9a825e | ||
|
|
bdff6b2e1c | ||
|
|
f39a451192 | ||
|
|
9af6de8d36 |
33
.travis.yml
33
.travis.yml
@@ -1,17 +1,34 @@
|
|||||||
language: ruby
|
language: ruby
|
||||||
cache: bundler
|
cache: bundler
|
||||||
rvm:
|
rvm:
|
||||||
- 2.5
|
- 2.5
|
||||||
- 2.4
|
- 2.4
|
||||||
- 2.3
|
|
||||||
gemfile:
|
gemfile:
|
||||||
- gemfiles/rails_52.gemfile
|
- gemfiles/rails_52.gemfile
|
||||||
- gemfiles/rails_51.gemfile
|
- gemfiles/rails_51.gemfile
|
||||||
- gemfiles/rails_50.gemfile
|
matrix:
|
||||||
- gemfiles/rails_42.gemfile
|
include:
|
||||||
|
# old rails
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_50.gemfile
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_42.gemfile
|
||||||
|
# old rubies
|
||||||
|
- rvm: 2.3
|
||||||
|
gemfile: gemfiles/rails_52.gemfile
|
||||||
|
# future rails
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_60.gemfile
|
||||||
|
# Allow rails 6 to fail while master branch is used
|
||||||
|
allow_failures:
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_60.gemfile
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
||||||
# for 2.5.0 until 2.5.1 is released: https://github.com/travis-ci/travis-ci/issues/8978
|
# for 2.5.0 until 2.5.1 is released:
|
||||||
|
# https://github.com/travis-ci/travis-ci/issues/8978
|
||||||
|
# https://github.com/travis-ci/travis-ci/issues/8969#issuecomment-354135622
|
||||||
before_install:
|
before_install:
|
||||||
- gem update --system
|
- gem update --system
|
||||||
|
- gem install bundler
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
appraise 'rails-60' do
|
||||||
|
git 'https://github.com/rails/rails.git' do
|
||||||
|
gem 'actionpack', '~> 6.0.0.alpha'
|
||||||
|
gem 'railties', '~> 6.0.0.alpha'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
appraise 'rails-52' do
|
appraise 'rails-52' do
|
||||||
gem 'actionpack', '~> 5.2.0.beta2'
|
gem 'actionpack', '~> 5.2.0.beta2'
|
||||||
gem 'railties', '~> 5.2.0.beta2'
|
gem 'railties', '~> 5.2.0.beta2'
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
# Unreleased
|
# 0.13.1
|
||||||
|
|
||||||
|
- Extracted typed response mappings to telegram-bot-types gem.
|
||||||
|
It now provides definitions for all API v3.5 methods.
|
||||||
|
- Fix #chat for non-message updates with TypedUpdates enabled
|
||||||
|
|
||||||
# 0.13.0
|
# 0.13.0
|
||||||
|
|
||||||
|
|||||||
2
Gemfile
2
Gemfile
@@ -8,7 +8,7 @@ group :development do
|
|||||||
gem 'pry-byebug', '~> 3.2.0'
|
gem 'pry-byebug', '~> 3.2.0'
|
||||||
gem 'sdoc', '~> 0.4.1'
|
gem 'sdoc', '~> 0.4.1'
|
||||||
|
|
||||||
gem 'telegram-bot-types', '~> 0.3.0'
|
gem 'telegram-bot-types', '~> 0.6.0'
|
||||||
|
|
||||||
gem 'rspec', '~> 3.5.0'
|
gem 'rspec', '~> 3.5.0'
|
||||||
gem 'rspec-its', '~> 1.1.0'
|
gem 'rspec-its', '~> 1.1.0'
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ in development, but you can use poller in production if you want.
|
|||||||
|
|
||||||
Package contains:
|
Package contains:
|
||||||
|
|
||||||
- Ligthweight client for bot API (with fast and thread-safe
|
- Lightweight client for bot API (with fast and thread-safe
|
||||||
[httpclient](https://github.com/nahi/httpclient) under the hood).
|
[httpclient](https://github.com/nahi/httpclient) under the hood).
|
||||||
- Controller with message parser: define methods for commands, not `case` branches.
|
- Controller with message parser: define methods for commands, not `case` branches.
|
||||||
- Middleware and routes helpers for production env.
|
- Middleware and routes helpers for production env.
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
|||||||
23
gemfiles/rails_60.gemfile
Обычный файл
23
gemfiles/rails_60.gemfile
Обычный файл
@@ -0,0 +1,23 @@
|
|||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
git "https://github.com/rails/rails.git" do
|
||||||
|
gem "actionpack", "~> 6.0.0.alpha"
|
||||||
|
gem "railties", "~> 6.0.0.alpha"
|
||||||
|
end
|
||||||
|
|
||||||
|
group :development do
|
||||||
|
gem "appraisal", "~> 2.2"
|
||||||
|
gem "pry", "~> 0.10.1"
|
||||||
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
|
gem "sdoc", "~> 0.4.1"
|
||||||
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
|
gem "rspec", "~> 3.5.0"
|
||||||
|
gem "rspec-its", "~> 1.1.0"
|
||||||
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
gem "rubocop", "~> 0.52.1"
|
||||||
|
gem "coveralls", "~> 0.8.2", require: false
|
||||||
|
end
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
@@ -1,35 +1,23 @@
|
|||||||
|
require 'telegram/bot/client/api_helper'
|
||||||
|
require 'active_support/core_ext/string/inflections'
|
||||||
|
|
||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
class Client
|
class Client
|
||||||
# Actions with type-casted results. Install `telegram-bot-types` gem first.
|
# Actions with type-casted results. Install `telegram-bot-types` gem first.
|
||||||
module TypedResponse
|
module TypedResponse
|
||||||
{
|
# First we define methods for every available api method to return `result`
|
||||||
getFile: :File,
|
# field instead of object.
|
||||||
getMe: :User,
|
ApiHelper.methods_list.each do |method|
|
||||||
getUpdates: [:Update],
|
define_method(method.to_s.underscore) do |*args|
|
||||||
getUserProfilePhotos: :UserProfilePhotos,
|
super(*args)['result']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
forwardMessage: :Message,
|
# And then override some of them which has castable results.
|
||||||
sendAudio: :Message,
|
Types::Response::WRAPPED_METHODS.each do |method, type|
|
||||||
sendDocument: :Message,
|
define_method(method.to_s.underscore) do |*args|
|
||||||
sendLocation: :Message,
|
Types::Response.wrap(super(*args)['result'], type)
|
||||||
sendMessage: :Message,
|
|
||||||
sendPhoto: :Message,
|
|
||||||
sendSticker: :Message,
|
|
||||||
sendVideo: :Message,
|
|
||||||
sendVoice: :Message,
|
|
||||||
}.each do |method, type|
|
|
||||||
next unless type
|
|
||||||
if type.is_a?(Array)
|
|
||||||
type_class = Types.const_get(type.first)
|
|
||||||
define_method(method.to_s.underscore) do |*args|
|
|
||||||
request(method, *args)['result'].map { |x| type_class.new(x) }
|
|
||||||
end
|
|
||||||
else
|
|
||||||
type_class = Types.const_get(type)
|
|
||||||
define_method(method.to_s.underscore) do |*args|
|
|
||||||
type_class.new request(method, *args)['result']
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
require 'active_support/core_ext/hash/keys'
|
require 'active_support/core_ext/hash/keys'
|
||||||
require 'active_support/core_ext/hash/transform_values'
|
|
||||||
require 'active_support/core_ext/hash/indifferent_access'
|
require 'active_support/core_ext/hash/indifferent_access'
|
||||||
|
|
||||||
module Telegram
|
module Telegram
|
||||||
@@ -43,7 +42,7 @@ module Telegram
|
|||||||
|
|
||||||
# Hash of botan clients made from #bots.
|
# Hash of botan clients made from #bots.
|
||||||
def botans
|
def botans
|
||||||
@botans ||= bots.transform_values(&:botan)
|
@botans ||= bots.map { |k, v| [k, v.botan] }.to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns config for .bots method. By default uses `telegram['bots']` section
|
# Returns config for .bots method. By default uses `telegram['bots']` section
|
||||||
|
|||||||
@@ -157,7 +157,14 @@ module Telegram
|
|||||||
#
|
#
|
||||||
# Can be overriden with `chat` option for #initialize.
|
# Can be overriden with `chat` option for #initialize.
|
||||||
def chat
|
def chat
|
||||||
@_chat ||= payload.try! { |x| x['chat'] || x['message'] && x['message']['chat'] }
|
@_chat ||=
|
||||||
|
if payload
|
||||||
|
if payload.is_a?(Hash)
|
||||||
|
payload['chat'] || payload['message'] && payload['message']['chat']
|
||||||
|
else
|
||||||
|
payload.try(:chat) || payload.try(:message).try!(:chat)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
|
|||||||
def deep_stringify(input)
|
def deep_stringify(input)
|
||||||
case input
|
case input
|
||||||
when Array then input.map(&method(__callee__))
|
when Array then input.map(&method(__callee__))
|
||||||
when Hash then input.transform_keys(&:to_s).transform_values(&method(__callee__))
|
when Hash then input.map { |k, v| [k.to_s, deep_stringify(v)] }.to_h
|
||||||
else input
|
else input
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
VERSION = '0.13.0'.freeze
|
VERSION = '0.13.1'.freeze
|
||||||
|
|
||||||
def self.gem_version
|
def self.gem_version
|
||||||
Gem::Version.new VERSION
|
Gem::Version.new VERSION
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
|
|||||||
|
|
||||||
describe '#get_me' do
|
describe '#get_me' do
|
||||||
subject { bot.get_me }
|
subject { bot.get_me }
|
||||||
before { expect(bot).to receive(:request).with(:getMe) { response } }
|
before { expect(bot).to receive(:request).with('getMe') { response } }
|
||||||
let(:response) { {'ok' => true, 'result' => {'id' => user_id}} }
|
let(:response) { {'ok' => true, 'result' => {'id' => user_id}} }
|
||||||
let(:user_id) { 123 }
|
let(:user_id) { 123 }
|
||||||
it { should be_instance_of Telegram::Bot::Types::User }
|
it { should be_instance_of Telegram::Bot::Types::User }
|
||||||
@@ -17,7 +17,7 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
|
|||||||
|
|
||||||
describe '#get_updates' do
|
describe '#get_updates' do
|
||||||
subject { bot.get_updates }
|
subject { bot.get_updates }
|
||||||
before { expect(bot).to receive(:request).with(:getUpdates) { response } }
|
before { expect(bot).to receive(:request).with('getUpdates') { response } }
|
||||||
let(:response) { {'ok' => true, 'result' => [{'update_id' => update_id}]} }
|
let(:response) { {'ok' => true, 'result' => [{'update_id' => update_id}]} }
|
||||||
let(:update_id) { 123 }
|
let(:update_id) { 123 }
|
||||||
it { should be_instance_of Array }
|
it { should be_instance_of Array }
|
||||||
@@ -29,4 +29,16 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
|
|||||||
it { expect { subject }.to raise_error Telegram::Bot::Error }
|
it { expect { subject }.to raise_error Telegram::Bot::Error }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#delete_webhook' do
|
||||||
|
subject { bot.delete_webhook }
|
||||||
|
before { expect(bot).to receive(:request).with('deleteWebhook') { response } }
|
||||||
|
let(:response) { {'ok' => true, 'result' => true} }
|
||||||
|
it { should eq true }
|
||||||
|
|
||||||
|
context 'on error' do
|
||||||
|
let(:response) { raise Telegram::Bot::Error }
|
||||||
|
it { expect { subject }.to raise_error Telegram::Bot::Error }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,17 +9,40 @@ RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do
|
|||||||
|
|
||||||
context 'when `update` is a virtus model' do
|
context 'when `update` is a virtus model' do
|
||||||
subject { controller }
|
subject { controller }
|
||||||
%w[
|
unique_types = Telegram::Bot::UpdatesController::PAYLOAD_TYPES - %w[
|
||||||
message
|
edited_message
|
||||||
inline_query
|
channel_post
|
||||||
chosen_inline_result
|
edited_channel_post
|
||||||
].each do |type|
|
]
|
||||||
|
unique_types.each do |type|
|
||||||
context "with #{type}" do
|
context "with #{type}" do
|
||||||
type_class = Telegram::Bot::Types.const_get(type.camelize)
|
type_class = Telegram::Bot::Types.const_get(type.camelize)
|
||||||
let(:payload_type) { type }
|
let(:payload_type) { type }
|
||||||
let(:payload) { {} }
|
let(:payload) do
|
||||||
|
{}.tap do |result|
|
||||||
|
result[:chat] = chat if type_class.instance_methods.include?(:chat)
|
||||||
|
result[:from] = from if type_class.instance_methods.include?(:from)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
let(:chat) { {id: 'chat_id'} }
|
||||||
|
let(:from) { {id: 'from_id'} }
|
||||||
its(:payload_type) { should eq payload_type }
|
its(:payload_type) { should eq payload_type }
|
||||||
its(:payload) { should be_instance_of type_class }
|
its(:payload) { should be_instance_of type_class }
|
||||||
|
|
||||||
|
if type_class.instance_methods.include?(:chat)
|
||||||
|
# Virtus does not support ==. :(
|
||||||
|
its(:chat) { should be_instance_of Telegram::Bot::Types::Chat }
|
||||||
|
its('chat.to_hash') { should include chat }
|
||||||
|
else
|
||||||
|
its(:chat) { should eq nil }
|
||||||
|
end
|
||||||
|
|
||||||
|
if type_class.instance_methods.include?(:from)
|
||||||
|
its(:from) { should be_instance_of Telegram::Bot::Types::User }
|
||||||
|
its('from.to_hash') { should include from }
|
||||||
|
else
|
||||||
|
its(:from) { should eq nil }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user