telegram-bot-ruby 0.3.11 → 0.4.0

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: 4556db3d1e5de794e842e522c6eb0a7e588022c6
4
- data.tar.gz: 2fc8acce5c5d5ae4188c70b1394a175375bd72d9
3
+ metadata.gz: 2f97dd430cc9b267ecc9bb00d55bd50dfb34b091
4
+ data.tar.gz: cfff29e4848daad38fdd280e968f62f31b987ee8
5
5
  SHA512:
6
- metadata.gz: dbbd6285c05adb6ae4e3c0e9b08846104b60b2337347261615d2e1a663fa228761d1fbf508c30e98095029affc9c70f7a379da30bc0ab3d02b096582904587dd
7
- data.tar.gz: 0eefaab43ecfb56f2c1c50dcd470342527552bda97b381c07b836e122d22899694636a2c2c4a1420e8288373996e7dda058da3d30dd0d01ae25bf501ec73b770
6
+ metadata.gz: f9763fcfe74b8e3bb067cd923182ebaa3418a7b68e9f71719baa8cf2a437810f3f58c8cefd76a8caaabb3eb1b8188fc7f0453213cf57d634aa841e7d8fade106
7
+ data.tar.gz: 9530676f3ee98dbb7edbf0901bf3a97a4a010f00209afdb7316f189e1f8c2b6df255761e3ca8de3e8c1a6b97fb6cef30c71bdf4fabc4da750621bcb46d8972c0
data/README.md CHANGED
@@ -50,6 +50,10 @@ Note that `bot.api` object implements [Telegram Bot API methods](https://core.te
50
50
 
51
51
  Same thing about `message` object - it implements [Message](https://core.telegram.org/bots/api#message) spec, so you always know what to expect from it.
52
52
 
53
+ ## Webhooks
54
+
55
+ If you are going to use [webhooks](https://core.telegram.org/bots/api#setwebhook) instead of [long polling](https://core.telegram.org/bots/api#getupdates), you need to implement your own webhook callbacks server. Take a look at [this repo](https://github.com/solyaris/BOTServer) as an example.
56
+
53
57
  ## Custom keyboards
54
58
 
55
59
  You can use your own [custom keyboards](https://core.telegram.org/bots#keyboards). Here is an example:
@@ -72,6 +76,31 @@ bot.listen do |message|
72
76
  end
73
77
  ```
74
78
 
79
+ ## Inline bots
80
+
81
+ If you are going to create [inline bot](https://core.telegram.org/bots/inline), check the example below:
82
+
83
+ ```ruby
84
+ bot.listen do |message|
85
+ case message
86
+ when Telegram::Bot::Types::InlineQuery
87
+ results = [
88
+ Telegram::Bot::Types::InlineQueryResultArticle
89
+ .new(id: 1, title: 'First article', message_text: 'Very interesting text goes here.'),
90
+ Telegram::Bot::Types::InlineQueryResultArticle
91
+ .new(id: 2, title: 'Second article', message_text: 'Another interesting text here.')
92
+ ]
93
+ bot.api.answer_inline_query(inline_query_id: message.id, results: results)
94
+ when Telegram::Bot::Types::Message
95
+ bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}!")
96
+ end
97
+ end
98
+ ```
99
+
100
+ Now, with `inline` mode enabled, your `message` object can be an instance of [Message](https://core.telegram.org/bots/api#message), [InlineQuery](https://core.telegram.org/bots/api#inlinequery) or [ChosenInlineResult](https://core.telegram.org/bots/api#choseninlineresult). That's why you need to check type of each message and decide how to handle it.
101
+
102
+ Using `answer_inline_query` you can send query results to user. `results` field must be an array of [query result objects](https://core.telegram.org/bots/api#inlinequeryresult).
103
+
75
104
  ## File upload
76
105
 
77
106
  Your bot can even upload files ([photos](https://core.telegram.org/bots/api#sendphoto), [audio](https://core.telegram.org/bots/api#sendaudio), [documents](https://core.telegram.org/bots/api#senddocument), [stickers](https://core.telegram.org/bots/api#sendsticker), [video](https://core.telegram.org/bots/api#sendvideo)) to Telegram servers. Just like this:
@@ -133,6 +162,11 @@ Sometimes you need to do some heavy work in another thread and send response fro
133
162
  $ TELEGRAM_BOT_POOL_SIZE=4 ruby bot.rb
134
163
  ```
135
164
 
165
+ ## Boilerplates
166
+
167
+ 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:
168
+ - [Ruby Telegram Bot boilerplate](https://github.com/telegram-bots/ruby-telegram-bot-boilerplate)
169
+
136
170
  ## Contributing
137
171
 
138
172
  1. Fork it
@@ -6,13 +6,20 @@ module Telegram
6
6
  ENDPOINTS = %w(
7
7
  getMe sendMessage forwardMessage sendPhoto sendAudio sendDocument
8
8
  sendSticker sendVideo sendVoice sendLocation sendChatAction
9
- getUserProfilePhotos getUpdates setWebhook getFile
9
+ getUserProfilePhotos getUpdates setWebhook getFile answerInlineQuery
10
10
  ).freeze
11
11
  REPLY_MARKUP_TYPES = [
12
12
  Telegram::Bot::Types::ReplyKeyboardMarkup,
13
13
  Telegram::Bot::Types::ReplyKeyboardHide,
14
14
  Telegram::Bot::Types::ForceReply
15
15
  ].freeze
16
+ INLINE_QUERY_RESULT_TYPES = [
17
+ Telegram::Bot::Types::InlineQueryResultArticle,
18
+ Telegram::Bot::Types::InlineQueryResultGif,
19
+ Telegram::Bot::Types::InlineQueryResultMpeg4Gif,
20
+ Telegram::Bot::Types::InlineQueryResultPhoto,
21
+ Telegram::Bot::Types::InlineQueryResultVideo
22
+ ].freeze
16
23
  POOL_SIZE = ENV.fetch('TELEGRAM_BOT_POOL_SIZE', 1).to_i.freeze
17
24
 
18
25
  attr_reader :token
@@ -53,7 +60,7 @@ module Telegram
53
60
  end
54
61
 
55
62
  def sanitize_value(value)
56
- jsonify_reply_markup(value)
63
+ jsonify_inline_query_results(jsonify_reply_markup(value))
57
64
  end
58
65
 
59
66
  def jsonify_reply_markup(value)
@@ -61,6 +68,11 @@ module Telegram
61
68
  value.to_h.to_json
62
69
  end
63
70
 
71
+ def jsonify_inline_query_results(value)
72
+ return value unless Array(value).all? { |i| INLINE_QUERY_RESULT_TYPES.include?(i.class) }
73
+ value.map { |i| i.to_h.select { |_, v| v } }.to_json
74
+ end
75
+
64
76
  def camelize(method_name)
65
77
  words = method_name.split('_')
66
78
  words.drop(1).map(&:capitalize!)
@@ -37,8 +37,9 @@ module Telegram
37
37
  response['result'].each do |data|
38
38
  update = Types::Update.new(data)
39
39
  @offset = update.update_id.next
40
- log_incoming_message(update.message)
41
- yield update.message
40
+ message = extract_message(update)
41
+ log_incoming_message(message)
42
+ yield message
42
43
  end
43
44
  rescue *TIMEOUT_EXCEPTIONS
44
45
  retry
@@ -50,13 +51,13 @@ module Telegram
50
51
  { offset: 0, timeout: 20, logger: NullLogger.new }
51
52
  end
52
53
 
54
+ def extract_message(update)
55
+ update.inline_query || update.chosen_inline_result || update.message
56
+ end
57
+
53
58
  def log_incoming_message(message)
54
59
  logger.info(
55
- format(
56
- 'Incoming message: text="%s" uid=%i',
57
- message.text,
58
- message.from.id
59
- )
60
+ format('Incoming message: text="%s" uid=%i', message, message.from.id)
60
61
  )
61
62
  end
62
63
  end
@@ -14,7 +14,7 @@ module Telegram
14
14
  end
15
15
 
16
16
  def error_code
17
- data[:error_code] || data["error_code"]
17
+ data[:error_code] || data['error_code']
18
18
  end
19
19
 
20
20
  private
@@ -10,6 +10,13 @@ require 'telegram/bot/types/contact'
10
10
  require 'telegram/bot/types/location'
11
11
  require 'telegram/bot/types/chat'
12
12
  require 'telegram/bot/types/message'
13
+ require 'telegram/bot/types/inline_query'
14
+ require 'telegram/bot/types/inline_query_result_article'
15
+ require 'telegram/bot/types/inline_query_result_photo'
16
+ require 'telegram/bot/types/inline_query_result_gif'
17
+ require 'telegram/bot/types/inline_query_result_mpeg4_gif'
18
+ require 'telegram/bot/types/inline_query_result_video'
19
+ require 'telegram/bot/types/chosen_inline_result'
13
20
  require 'telegram/bot/types/update'
14
21
  require 'telegram/bot/types/reply_keyboard_markup'
15
22
  require 'telegram/bot/types/reply_keyboard_hide'
@@ -0,0 +1,13 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class ChosenInlineResult < Base
5
+ attribute :result_id, String
6
+ attribute :from, User
7
+ attribute :query, String
8
+
9
+ alias_method :to_s, :query
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class InlineQuery < Base
5
+ attribute :id, String
6
+ attribute :from, User
7
+ attribute :query, String
8
+ attribute :offset, String
9
+
10
+ alias_method :to_s, :query
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class InlineQueryResultArticle < Base
5
+ attribute :type, String, default: 'article'
6
+ attribute :id, String
7
+ attribute :title, String
8
+ attribute :message_text, String
9
+ attribute :parse_mode, String
10
+ attribute :parse_mode, String
11
+ attribute :disable_web_page_preview, Boolean
12
+ attribute :url, String
13
+ attribute :hide_url, Boolean
14
+ attribute :description, String
15
+ attribute :thumb_url, String
16
+ attribute :thumb_width, Integer
17
+ attribute :thumb_height, Integer
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class InlineQueryResultGif < Base
5
+ attribute :type, String, default: 'gif'
6
+ attribute :id, String
7
+ attribute :gif_url, String
8
+ attribute :gif_width, Integer
9
+ attribute :gif_height, Integer
10
+ attribute :thumb_url, String
11
+ attribute :title, String
12
+ attribute :caption, String
13
+ attribute :message_text, String
14
+ attribute :parse_mode, String
15
+ attribute :disable_web_page_preview, Boolean
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class InlineQueryResultMpeg4Gif < Base
5
+ attribute :type, String, default: 'mpeg4_gif'
6
+ attribute :id, String
7
+ attribute :mpeg4_url, String
8
+ attribute :mpeg4_width, Integer
9
+ attribute :mpeg4_height, Integer
10
+ attribute :thumb_url, String
11
+ attribute :title, String
12
+ attribute :caption, String
13
+ attribute :message_text, String
14
+ attribute :parse_mode, String
15
+ attribute :disable_web_page_preview, Boolean
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class InlineQueryResultPhoto < Base
5
+ attribute :type, String, default: 'photo'
6
+ attribute :id, String
7
+ attribute :photo_url, String
8
+ attribute :photo_width, Integer
9
+ attribute :photo_height, Integer
10
+ attribute :thumb_url, String
11
+ attribute :title, String
12
+ attribute :description, String
13
+ attribute :caption, String
14
+ attribute :message_text, String
15
+ attribute :parse_mode, String
16
+ attribute :disable_web_page_preview, Boolean
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Telegram
2
+ module Bot
3
+ module Types
4
+ class InlineQueryResultVideo < Base
5
+ attribute :type, String, default: 'video'
6
+ attribute :id, String
7
+ attribute :video_url, String
8
+ attribute :mime_type, String
9
+ attribute :message_text, String
10
+ attribute :parse_mode, String
11
+ attribute :disable_web_page_preview, Boolean
12
+ attribute :video_width, Integer
13
+ attribute :video_height, Integer
14
+ attribute :video_duration, Integer
15
+ attribute :thumb_url, String
16
+ attribute :title, String
17
+ attribute :description, String
18
+ end
19
+ end
20
+ end
21
+ end
@@ -25,6 +25,8 @@ module Telegram
25
25
  attribute :delete_chat_photo, Boolean
26
26
  attribute :group_chat_created, Boolean
27
27
  attribute :chat, Chat
28
+
29
+ alias_method :to_s, :text
28
30
  end
29
31
  end
30
32
  end
@@ -4,6 +4,8 @@ module Telegram
4
4
  class Update < Base
5
5
  attribute :update_id, Integer
6
6
  attribute :message, Message
7
+ attribute :inline_query, InlineQuery
8
+ attribute :chosen_inline_result, ChosenInlineResult
7
9
  end
8
10
  end
9
11
  end
@@ -1,5 +1,5 @@
1
1
  module Telegram
2
2
  module Bot
3
- VERSION = '0.3.11'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-bot-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.11
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Tipugin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-22 00:00:00.000000000 Z
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httmultiparty
@@ -137,10 +137,17 @@ files:
137
137
  - lib/telegram/bot/types/audio.rb
138
138
  - lib/telegram/bot/types/base.rb
139
139
  - lib/telegram/bot/types/chat.rb
140
+ - lib/telegram/bot/types/chosen_inline_result.rb
140
141
  - lib/telegram/bot/types/contact.rb
141
142
  - lib/telegram/bot/types/document.rb
142
143
  - lib/telegram/bot/types/file.rb
143
144
  - lib/telegram/bot/types/force_reply.rb
145
+ - lib/telegram/bot/types/inline_query.rb
146
+ - lib/telegram/bot/types/inline_query_result_article.rb
147
+ - lib/telegram/bot/types/inline_query_result_gif.rb
148
+ - lib/telegram/bot/types/inline_query_result_mpeg4_gif.rb
149
+ - lib/telegram/bot/types/inline_query_result_photo.rb
150
+ - lib/telegram/bot/types/inline_query_result_video.rb
144
151
  - lib/telegram/bot/types/location.rb
145
152
  - lib/telegram/bot/types/message.rb
146
153
  - lib/telegram/bot/types/photo_size.rb