telegram-bot-ruby 0.5.0.beta1 → 0.5.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -0
- data/lib/telegram/bot/api.rb +2 -2
- data/lib/telegram/bot/types/base.rb +1 -1
- data/lib/telegram/bot/types/inline_keyboard_button.rb +0 -4
- data/lib/telegram/bot/types/inline_keyboard_markup.rb +6 -2
- data/lib/telegram/bot/types/keyboard_button.rb +0 -4
- data/lib/telegram/bot/types/reply_keyboard_markup.rb +6 -2
- data/lib/telegram/bot/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21c521862fb67cf5de03a54c65f4b459350ad9d2
|
4
|
+
data.tar.gz: fd0095bc8c4815329289f92af0fbf3ec292ea830
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55b5682ccc05202ff57ff36d12f4d762c5083253a1b074e1b330294a7fe0918e5929c7e2aed7f902e66c45ffe7cfd092915cf66071d7fe56d016c5033eae4b15
|
7
|
+
data.tar.gz: 7c2468fbe973b32ceb7f144f3c886122dc35ff79950c86b95477b7f3f8cafb0bb094cd757e819aa37e73c41b1e630f29c0c1f3baabf8cf2026b2c68dac3f8022
|
data/README.md
CHANGED
@@ -77,6 +77,43 @@ bot.listen do |message|
|
|
77
77
|
end
|
78
78
|
```
|
79
79
|
|
80
|
+
Furthermore, you can ask user to share location or phone number using `KeyboardButton`:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
bot.listen do |message|
|
84
|
+
kb = [
|
85
|
+
Telegram::Bot::Types::KeyboardButton.new(text: 'Give me your phone number', request_contact: true),
|
86
|
+
Telegram::Bot::Types::KeyboardButton.new(text: 'Show me your location', request_location: true)
|
87
|
+
]
|
88
|
+
markup = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: kb)
|
89
|
+
bot.api.send_message(chat_id: message.chat.id, text: 'Hey!', reply_markup: markup)
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
## Inline keyboards
|
94
|
+
|
95
|
+
[Bot API 2.0](https://core.telegram.org/bots/2-0-intro) brought us new inline keyboards. Example:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
bot.listen do |message|
|
99
|
+
case message
|
100
|
+
when Telegram::Bot::Types::CallbackQuery
|
101
|
+
# Here you can handle your callbacks from inline buttons
|
102
|
+
if message.data == 'touch'
|
103
|
+
bot.api.send_message(chat_id: message.from.id, text: "Don't touch me!")
|
104
|
+
end
|
105
|
+
when Telegram::Bot::Types::Message
|
106
|
+
kb = [
|
107
|
+
Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Go to Google', url: 'https://google.com'),
|
108
|
+
Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Touch me', callback_data: 'touch'),
|
109
|
+
Telegram::Bot::Types::InlineKeyboardButton.new(text: 'Switch to inline', switch_inline_query: 'some text')
|
110
|
+
]
|
111
|
+
markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
|
112
|
+
bot.api.send_message(chat_id: message.chat.id, text: 'Make a choice', reply_markup: markup)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
80
117
|
## Inline bots
|
81
118
|
|
82
119
|
If you are going to create [inline bot](https://core.telegram.org/bots/inline), check the example below:
|
@@ -155,6 +192,18 @@ end
|
|
155
192
|
- Telegram's user id (required)
|
156
193
|
- hash of additional properties (optional)
|
157
194
|
|
195
|
+
## Connection adapters
|
196
|
+
|
197
|
+
Since version `0.5.0` we rely on [faraday](https://github.com/lostisland/faraday) under the hood. You can use any of supported adapters (for example, `net/http/persistent`):
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
require 'net/http/persistent'
|
201
|
+
|
202
|
+
Telegram::Bot.configure do |config|
|
203
|
+
config.adapter = :net_http_persistent
|
204
|
+
end
|
205
|
+
```
|
206
|
+
|
158
207
|
## Boilerplates
|
159
208
|
|
160
209
|
If you don't know how to setup database for your bot or how to use it with different languages here are some boilerplates which can help you to start faster:
|
data/lib/telegram/bot/api.rb
CHANGED
@@ -81,12 +81,12 @@ module Telegram
|
|
81
81
|
|
82
82
|
def jsonify_reply_markup(value)
|
83
83
|
return value unless REPLY_MARKUP_TYPES.include?(value.class)
|
84
|
-
value.
|
84
|
+
value.to_compact_hash.to_json
|
85
85
|
end
|
86
86
|
|
87
87
|
def jsonify_inline_query_results(value)
|
88
88
|
return value unless value.is_a?(Array) && value.all? { |i| INLINE_QUERY_RESULT_TYPES.include?(i.class) }
|
89
|
-
value.map { |i| i.
|
89
|
+
value.map { |i| i.to_compact_hash.select { |_, v| v } }.to_json
|
90
90
|
end
|
91
91
|
|
92
92
|
def camelize(method_name)
|
@@ -4,9 +4,13 @@ module Telegram
|
|
4
4
|
class InlineKeyboardMarkup < Base
|
5
5
|
attribute :inline_keyboard, Array[Array[InlineKeyboardButton]]
|
6
6
|
|
7
|
-
def
|
7
|
+
def to_compact_hash
|
8
8
|
hsh = super
|
9
|
-
hsh[:inline_keyboard].map!
|
9
|
+
hsh[:inline_keyboard].map! do |arr|
|
10
|
+
arr.map do |item|
|
11
|
+
item.is_a?(InlineKeyboardButton) ? item.to_compact_hash : item
|
12
|
+
end
|
13
|
+
end
|
10
14
|
|
11
15
|
hsh
|
12
16
|
end
|
@@ -7,9 +7,13 @@ module Telegram
|
|
7
7
|
attribute :one_time_keyboard, Boolean, default: false
|
8
8
|
attribute :selective, Boolean, default: false
|
9
9
|
|
10
|
-
def
|
10
|
+
def to_compact_hash
|
11
11
|
hsh = super
|
12
|
-
hsh[:keyboard].map!
|
12
|
+
hsh[:keyboard].map! do |arr|
|
13
|
+
arr.map do |item|
|
14
|
+
item.is_a?(KeyboardButton) ? item.to_compact_hash : item
|
15
|
+
end
|
16
|
+
end
|
13
17
|
|
14
18
|
hsh
|
15
19
|
end
|
data/lib/telegram/bot/version.rb
CHANGED