telegrambot 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.gitlab-ci.yml +25 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/Guardfile +6 -0
- data/LICENSE +674 -0
- data/README.md +74 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/telegrambot.rb +9 -0
- data/lib/telegrambot/api.rb +89 -0
- data/lib/telegrambot/client.rb +18 -0
- data/lib/telegrambot/exceptions.rb +1 -0
- data/lib/telegrambot/exceptions/response_error.rb +28 -0
- data/lib/telegrambot/types.rb +26 -0
- data/lib/telegrambot/types/audio.rb +10 -0
- data/lib/telegrambot/types/base.rb +18 -0
- data/lib/telegrambot/types/callback_query.rb +9 -0
- data/lib/telegrambot/types/chat.rb +10 -0
- data/lib/telegrambot/types/chat_member.rb +6 -0
- data/lib/telegrambot/types/contact.rb +8 -0
- data/lib/telegrambot/types/document.rb +9 -0
- data/lib/telegrambot/types/file.rb +7 -0
- data/lib/telegrambot/types/force_reply.rb +6 -0
- data/lib/telegrambot/types/inline_keyboard_button.rb +8 -0
- data/lib/telegrambot/types/inline_keyboard_markup.rb +15 -0
- data/lib/telegrambot/types/inline_query.rb +9 -0
- data/lib/telegrambot/types/inline_query_result_article.rb +15 -0
- data/lib/telegrambot/types/inline_query_result_audio.rb +12 -0
- data/lib/telegrambot/types/inline_query_result_cached_audio.rb +9 -0
- data/lib/telegrambot/types/inline_query_result_cached_document.rb +12 -0
- data/lib/telegrambot/types/inline_query_result_cached_gif.rb +11 -0
- data/lib/telegrambot/types/keyboard_button.rb +7 -0
- data/lib/telegrambot/types/location.rb +6 -0
- data/lib/telegrambot/types/message.rb +53 -0
- data/lib/telegrambot/types/message_entity.rb +9 -0
- data/lib/telegrambot/types/photo_size.rb +8 -0
- data/lib/telegrambot/types/reply_keyboard_hide.rb +6 -0
- data/lib/telegrambot/types/reply_keyboard_markup.rb +8 -0
- data/lib/telegrambot/types/sticker.rb +10 -0
- data/lib/telegrambot/types/update.rb +10 -0
- data/lib/telegrambot/types/user.rb +8 -0
- data/lib/telegrambot/types/user_profile_photos.rb +6 -0
- data/lib/telegrambot/types/venue.rb +8 -0
- data/lib/telegrambot/types/video.rb +11 -0
- data/lib/telegrambot/types/voice.rb +8 -0
- data/lib/telegrambot/version.rb +3 -0
- data/telegrambot.gemspec +41 -0
- metadata +250 -0
data/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Telegrambot [](https://travis-ci.org/mijailr/telegrambot)
|
|
2
|
+
(iba a escribir este readme en ingles, pero luego dije: "¿pa qué?")
|
|
3
|
+
|
|
4
|
+
Otro wrapper de Telegram, resulta que los que habían por allí no funcionaban como yo necesitaba, por lo que escribí esta a mi gusto.
|
|
5
|
+
|
|
6
|
+
## Instalacion
|
|
7
|
+
|
|
8
|
+
Se instala como cualquier `gem` via Gemfile:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'telegrambot'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Y luego:
|
|
15
|
+
|
|
16
|
+
$ bundle
|
|
17
|
+
|
|
18
|
+
O directamente con:
|
|
19
|
+
|
|
20
|
+
$ gem install telegrambot
|
|
21
|
+
|
|
22
|
+
## Uso
|
|
23
|
+
|
|
24
|
+
Muy simple de usar:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
# Configuras el token
|
|
28
|
+
bot_token = "213652360:AAGYiBuAslikmBFnO9zvgwns2ckWDVKOpJg"
|
|
29
|
+
bot = Telegrambot::Client.new(bot_token)
|
|
30
|
+
|
|
31
|
+
# Luego sólo llamas al endpoint de telegram en snake_case
|
|
32
|
+
updates = bot.api.get_updates
|
|
33
|
+
|
|
34
|
+
# O para enviar un mensaje
|
|
35
|
+
mensaje = "¡Mira! me envié un mensaje"
|
|
36
|
+
bot.api.send_message(chat_id: -160175187, text: mensaje)
|
|
37
|
+
|
|
38
|
+
# Incluso puedes enviar InlineKeyboard
|
|
39
|
+
boton = Telegrambot::Types::InlineKeyboardButton.new(
|
|
40
|
+
text: "Visita mi página",
|
|
41
|
+
url: "https://mijailr.com")
|
|
42
|
+
markup = Telegrambot::Types::InlineKeyboardMarkup.new(inline_keyboard: boton)
|
|
43
|
+
bot.api.send_message(chat_id: -160175187, text: mensaje, reply_markup: markup)
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
Como puedes ver todos los types declarados en la documentación de la api de Telegram tienen exactamente el mismo nombre aquí por lo que basta con ver esa documentación para saber que usar aquí.
|
|
47
|
+
|
|
48
|
+
Lo único que cambia son los enpoints que pasan de CamelCase a snake_case por lo que todo lo que llames con la api es asi.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### Para webhook
|
|
52
|
+
|
|
53
|
+
Para web hook es la misma lógica, si usas ruby on rails es más sensillo.
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
# Solo debes agregar a algún método de un controlador
|
|
57
|
+
def webhook
|
|
58
|
+
bot_token = "213652360:AAGYiBuAslikmBFnO9zvgwns2ckWDVKOpJg"
|
|
59
|
+
bot = Telegrambot::Client.new(bot_token)
|
|
60
|
+
mensaje = bot.listen_webhook(params)
|
|
61
|
+
# De aquí en adelante lo puedes usar como un objeto Telegrambot::Types::Message
|
|
62
|
+
# También puedes saber si el mensaje trae un `bot_command`
|
|
63
|
+
if mensaje.command?
|
|
64
|
+
mensaje.command
|
|
65
|
+
mensaje.attributes
|
|
66
|
+
# haces lo que necesites con mensaje.command y mensaje.attributes
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Si no usas rails, asegurate que `params` sea un hash.
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
[GNU GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "telegrambot/bot"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/telegrambot.rb
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module Telegrambot
|
|
2
|
+
class Api
|
|
3
|
+
|
|
4
|
+
attr_reader :token
|
|
5
|
+
|
|
6
|
+
REPLY_MARKUP_TYPES = [
|
|
7
|
+
Telegrambot::Types::ReplyKeyboardMarkup,
|
|
8
|
+
Telegrambot::Types::ReplyKeyboardHide,
|
|
9
|
+
Telegrambot::Types::ForceReply,
|
|
10
|
+
Telegrambot::Types::InlineKeyboardMarkup
|
|
11
|
+
].freeze
|
|
12
|
+
|
|
13
|
+
def initialize(token)
|
|
14
|
+
@token = token
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private_instance_methods :snakecase
|
|
18
|
+
# Convierte un camelCase a snake_case
|
|
19
|
+
# @return String
|
|
20
|
+
def self.snakecase(endpoint)
|
|
21
|
+
endpoint.to_s.gsub(/::/, '/').
|
|
22
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
23
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
|
24
|
+
tr("-", "_").
|
|
25
|
+
downcase
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Endpoints de Telegram Bot API (Metaprogramming)
|
|
30
|
+
# @see https://core.telegram.org/bots/api#available-methods
|
|
31
|
+
# @return [Hash]
|
|
32
|
+
# @raise [Telegrambot::Exceptions::ResponseError]
|
|
33
|
+
# @param raw_params [Hash] Hash de opciones de telegram
|
|
34
|
+
%w(
|
|
35
|
+
getUpdates setWebhook getMe sendMessage forwardMessage sendPhoto
|
|
36
|
+
sendAudio sendDocument sendSticker sendVideo sendVoice sendLocation
|
|
37
|
+
sendVenue sendContact sendChatAction getUserProfilePhotos getFile
|
|
38
|
+
kickChatMember unbanChatMember answerCallbackQuery editMessageText
|
|
39
|
+
editMessageCaption editMessageReplyMarkup answerInlineQuery getChat
|
|
40
|
+
leaveChat getChatAdministrators getChatMember getChatMembersCount
|
|
41
|
+
).map{ |e| snakecase(e) }.each do | endpoint |
|
|
42
|
+
define_method endpoint do | raw_params = {} |
|
|
43
|
+
params = build_params(raw_params)
|
|
44
|
+
response = rest_client.post("/bot#{token}/#{camelcase(endpoint)}", params)
|
|
45
|
+
raise Telegrambot::Exceptions::ResponseError.new(response) unless response.status == 200
|
|
46
|
+
JSON.parse(response.body)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# Convierte un snake_case a camelCase
|
|
53
|
+
# @return String
|
|
54
|
+
def camelcase(endpoint)
|
|
55
|
+
words = endpoint.split('_')
|
|
56
|
+
words.drop(1).map(&:capitalize!)
|
|
57
|
+
words.join
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def build_params(hash)
|
|
61
|
+
hash.each_with_object({}) do |(key, value), params|
|
|
62
|
+
params[key] = sanitize_value(value)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def sanitize_value(value)
|
|
67
|
+
json_inline_query_results(json_reply_markup(value))
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def json_reply_markup(value)
|
|
71
|
+
return value unless REPLY_MARKUP_TYPES.include?(value.class)
|
|
72
|
+
value.to_compact_hash.to_json
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def json_inline_query_results(value)
|
|
76
|
+
return value unless value.is_a?(Array) && value.all? { |i| INLINE_QUERY_RESULT_TYPES.include?(i.class) }
|
|
77
|
+
value.map { |i| i.to_compact_hash.select { |_, v| v } }.to_json
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def rest_client
|
|
81
|
+
Faraday.new(url: 'https://api.telegram.org') do |faraday|
|
|
82
|
+
faraday.request :multipart
|
|
83
|
+
faraday.request :url_encoded
|
|
84
|
+
faraday.adapter Faraday.default_adapter
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Telegrambot
|
|
2
|
+
class Client
|
|
3
|
+
attr_reader :api
|
|
4
|
+
|
|
5
|
+
def initialize(token)
|
|
6
|
+
@api = Api.new(token)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Escucha Webhooks
|
|
10
|
+
# @return [Telegrambot::Types::Update]
|
|
11
|
+
# @param update [Hash]
|
|
12
|
+
def listen_webhook(data)
|
|
13
|
+
update = Telegrambot::Types::Update.new(data)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'telegrambot/exceptions/response_error'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Telegrambot::Exceptions
|
|
2
|
+
class ResponseError < StandardError
|
|
3
|
+
attr_reader :response
|
|
4
|
+
|
|
5
|
+
def initialize(response)
|
|
6
|
+
@response = response
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_s
|
|
10
|
+
super +
|
|
11
|
+
format(' (%s)', data.map { |k, v| %(#{k}: "#{v}")}.join(', '))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def error_code
|
|
15
|
+
data[:error_code] || data['error_code']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def data
|
|
21
|
+
@data ||= begin
|
|
22
|
+
JSON.parse(response.body)
|
|
23
|
+
rescue JSON::ParseError
|
|
24
|
+
{ error_code: response.status, uri: response.env.url.to_s }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'telegrambot/types/base'
|
|
2
|
+
require 'telegrambot/types/user'
|
|
3
|
+
require 'telegrambot/types/chat'
|
|
4
|
+
require 'telegrambot/types/voice'
|
|
5
|
+
require 'telegrambot/types/message_entity'
|
|
6
|
+
require 'telegrambot/types/audio'
|
|
7
|
+
require 'telegrambot/types/photo_size'
|
|
8
|
+
require 'telegrambot/types/document'
|
|
9
|
+
require 'telegrambot/types/sticker'
|
|
10
|
+
require 'telegrambot/types/video'
|
|
11
|
+
require 'telegrambot/types/voice'
|
|
12
|
+
require 'telegrambot/types/contact'
|
|
13
|
+
require 'telegrambot/types/location'
|
|
14
|
+
require 'telegrambot/types/venue'
|
|
15
|
+
require 'telegrambot/types/message'
|
|
16
|
+
require 'telegrambot/types/user_profile_photos'
|
|
17
|
+
require 'telegrambot/types/file'
|
|
18
|
+
require 'telegrambot/types/keyboard_button'
|
|
19
|
+
require 'telegrambot/types/reply_keyboard_markup'
|
|
20
|
+
require 'telegrambot/types/reply_keyboard_hide'
|
|
21
|
+
require 'telegrambot/types/inline_keyboard_button'
|
|
22
|
+
require 'telegrambot/types/inline_keyboard_markup'
|
|
23
|
+
require 'telegrambot/types/callback_query'
|
|
24
|
+
require 'telegrambot/types/force_reply'
|
|
25
|
+
require 'telegrambot/types/chat_member'
|
|
26
|
+
require 'telegrambot/types/update'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Telegrambot::Types
|
|
2
|
+
class Base
|
|
3
|
+
include Virtus.model
|
|
4
|
+
|
|
5
|
+
# @return [Hash]
|
|
6
|
+
def to_compact_hash
|
|
7
|
+
Hash[attributes.dup.delete_if { |_, v| v.nil? }.map do |key, value|
|
|
8
|
+
value =
|
|
9
|
+
if value.class.ancestors.include?(Telegrambot::Types::Base)
|
|
10
|
+
value.to_compact_hash
|
|
11
|
+
else
|
|
12
|
+
value
|
|
13
|
+
end
|
|
14
|
+
[key, value]
|
|
15
|
+
end]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Telegrambot::Types
|
|
2
|
+
class InlineKeyboardMarkup < Base
|
|
3
|
+
attribute :inline_keyboard, Array[Array[InlineKeyboardButton]]
|
|
4
|
+
# @return [Hash]
|
|
5
|
+
def to_compact_hash
|
|
6
|
+
hsh = super
|
|
7
|
+
hsh[:inline_keyboard].map! do |arr|
|
|
8
|
+
arr.map do |item|
|
|
9
|
+
item.is_a?(InlineKeyboardButton) ? item.to_compact_hash : item
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
hsh
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Telegrambot::Type
|
|
2
|
+
class InlineQueryResultArticle < Base
|
|
3
|
+
attribute :type, String, default: 'article'
|
|
4
|
+
attribute :id, String
|
|
5
|
+
attribute :title, String
|
|
6
|
+
attribute :input_message_content, InputMessageContent
|
|
7
|
+
attribute :reply_markup, InlineKeyboardMarkup
|
|
8
|
+
attribute :url, String
|
|
9
|
+
attribute :hide_url, Boolean
|
|
10
|
+
attribute :description, String
|
|
11
|
+
attribute :thumb_url, String
|
|
12
|
+
attribute :thumb_width, Integer
|
|
13
|
+
attribute :thumb_height, Integer
|
|
14
|
+
end
|
|
15
|
+
end
|