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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 789950417db18c87aeeaf3ada4788131e8d1e5a0
4
- data.tar.gz: 8240d4073e39f8d7400df0682689e1aec49839d5
3
+ metadata.gz: 21c521862fb67cf5de03a54c65f4b459350ad9d2
4
+ data.tar.gz: fd0095bc8c4815329289f92af0fbf3ec292ea830
5
5
  SHA512:
6
- metadata.gz: 8c5819e6e65f453afc80df9354b21783912e3f1f76a15b4bd0fc2ceefe9b1a2472e8c04b661a4175a32e3d2adadf149ae061dd00c2ed5386e27621cf430cf424
7
- data.tar.gz: 28170029b9b8b8066e693f30687fa8eb3a3168d73a92ef35a18fcc5bf149ceee8358d767a0a4603899a1c3cac560ff9bf3519c6489cf9dd80ed04f7ef517dba5
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:
@@ -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.to_h.to_json
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.to_h.select { |_, v| v } }.to_json
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,7 +4,7 @@ module Telegram
4
4
  class Base
5
5
  include Virtus.model
6
6
 
7
- def compact_attributes
7
+ def to_compact_hash
8
8
  attributes.dup.delete_if { |_, v| v.nil? }
9
9
  end
10
10
  end
@@ -6,10 +6,6 @@ module Telegram
6
6
  attribute :url, String
7
7
  attribute :callback_data, String
8
8
  attribute :switch_inline_query, String
9
-
10
- def to_h
11
- super.delete_if { |_, v| v.nil? }
12
- end
13
9
  end
14
10
  end
15
11
  end
@@ -4,9 +4,13 @@ module Telegram
4
4
  class InlineKeyboardMarkup < Base
5
5
  attribute :inline_keyboard, Array[Array[InlineKeyboardButton]]
6
6
 
7
- def to_h
7
+ def to_compact_hash
8
8
  hsh = super
9
- hsh[:inline_keyboard].map! { |a| a.map(&:to_h) }
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
@@ -5,10 +5,6 @@ module Telegram
5
5
  attribute :text, String
6
6
  attribute :request_contact, Boolean
7
7
  attribute :request_location, Boolean
8
-
9
- def to_h
10
- super.delete_if { |_, v| v.nil? }
11
- end
12
8
  end
13
9
  end
14
10
  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 to_h
10
+ def to_compact_hash
11
11
  hsh = super
12
- hsh[:keyboard].map! { |a| a.map(&:to_h) }
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
@@ -1,5 +1,5 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.5.0.beta1'.freeze
3
+ VERSION = '0.5.0.beta2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-bot-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.beta1
4
+ version: 0.5.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Tipugin