telegram-bot-ruby 0.2.0 → 0.2.1
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 +4 -4
- data/README.md +28 -1
- data/lib/telegram/bot.rb +1 -1
- data/lib/telegram/bot/api.rb +26 -2
- data/lib/telegram/bot/types.rb +3 -0
- data/lib/telegram/bot/types/force_reply.rb +10 -0
- data/lib/telegram/bot/types/reply_keyboard_hide.rb +10 -0
- data/lib/telegram/bot/types/reply_keyboard_markup.rb +12 -0
- data/lib/telegram/bot/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d52d14ef44cbb3583be07ce94d6a497c5fcdbe9
|
4
|
+
data.tar.gz: a9e8eb1afb4a9ed07ff4a645f68f54f4978505d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f79b2bf808f6b88e2003a8eb628a88dc45d093aae9e67b05a8659de96f71d48b9f37418c09bb0eb9b0b6d20507cf0f183bedd8329a476af1e030dcd965d1b62
|
7
|
+
data.tar.gz: 50db3e10ebadbd7daa4cb0f877e9453a191a1b1a5bebf8b530f16971917dea7bffa32819eb22dd67bf26899cc5a237e7e364e1b84cca34f42a7e5808c4bfa4cd
|
data/README.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
Ruby wrapper for [Telegram's Bot API](https://core.telegram.org/bots/api).
|
4
4
|
|
5
|
+
[](http://badge.fury.io/rb/telegram-bot-ruby)
|
6
|
+
[](https://codeclimate.com/github/atipugin/telegram-bot-ruby)
|
7
|
+
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add following line to your Gemfile:
|
@@ -34,8 +37,10 @@ token = 'YOUR_TELEGRAM_BOT_API_TOKEN'
|
|
34
37
|
Telegram::Bot::Client.run(token) do |bot|
|
35
38
|
bot.listen do |message|
|
36
39
|
case message.text
|
37
|
-
when
|
40
|
+
when /^\/start$/
|
38
41
|
bot.api.sendMessage(chat_id: message.chat.id, text: "Hello, #{message.from.username}")
|
42
|
+
when /^\/stop$/
|
43
|
+
bot.api.sendMessage(chat_id: message.chat.id, text: "Bye, #{message.from.username}")
|
39
44
|
end
|
40
45
|
end
|
41
46
|
end
|
@@ -45,6 +50,28 @@ Note that `bot.api` object implements [Telegram Bot API methods](https://core.te
|
|
45
50
|
|
46
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.
|
47
52
|
|
53
|
+
## Custom keyboards
|
54
|
+
|
55
|
+
You can use your own [custom keyboards](https://core.telegram.org/bots#keyboards). Here is an example:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
bot.listen do |message|
|
59
|
+
case message.text
|
60
|
+
when /^\/start$/
|
61
|
+
question = 'London is a capital of which country?'
|
62
|
+
# See more: https://core.telegram.org/bots/api#replykeyboardmarkup
|
63
|
+
answers =
|
64
|
+
Telegram::Bot::Types::ReplyKeyboardMarkup
|
65
|
+
.new(keyboard: [%w(A B), %w(C D)], one_time_keyboard: true)
|
66
|
+
bot.api.sendMessage(chat_id: message.chat.id, text: question, reply_markup: answers)
|
67
|
+
when /^\/stop$/
|
68
|
+
# See more: https://core.telegram.org/bots/api#replykeyboardhide
|
69
|
+
kb = Telegram::Bot::Types::ReplyKeyboardHide.new(hide_keyboard: true)
|
70
|
+
bot.api.sendMessage(chat_id: message.chat.id, text: 'Sorry to see you go :(', reply_markup: kb)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
48
75
|
## Contributing
|
49
76
|
|
50
77
|
1. Fork it
|
data/lib/telegram/bot.rb
CHANGED
data/lib/telegram/bot/api.rb
CHANGED
@@ -8,6 +8,11 @@ module Telegram
|
|
8
8
|
sendSticker sendVideo sendLocation sendChatAction getUserProfilePhotos
|
9
9
|
getUpdates setWebhook
|
10
10
|
).freeze
|
11
|
+
REPLY_MARKUP_TYPES = [
|
12
|
+
Telegram::Bot::Types::ReplyKeyboardMarkup,
|
13
|
+
Telegram::Bot::Types::ReplyKeyboardHide,
|
14
|
+
Telegram::Bot::Types::ForceReply
|
15
|
+
].freeze
|
11
16
|
|
12
17
|
attr_reader :token
|
13
18
|
|
@@ -22,8 +27,27 @@ module Telegram
|
|
22
27
|
ENDPOINTS.include?(method_name.to_s) ? call(method_name, *args) : super
|
23
28
|
end
|
24
29
|
|
25
|
-
def call(endpoint,
|
26
|
-
|
30
|
+
def call(endpoint, raw_params = {})
|
31
|
+
params = build_params(raw_params)
|
32
|
+
response = self.class.get("/bot#{token}/#{endpoint}", query: params)
|
33
|
+
response.code == 200 ? response.to_h : {}
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def build_params(h)
|
39
|
+
h.each_with_object({}) do |(key, value), params|
|
40
|
+
params[key] = sanitize_value(value)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def sanitize_value(value)
|
45
|
+
jsonify_reply_markup(value)
|
46
|
+
end
|
47
|
+
|
48
|
+
def jsonify_reply_markup(value)
|
49
|
+
return value unless REPLY_MARKUP_TYPES.include?(value.class)
|
50
|
+
value.to_h.to_json
|
27
51
|
end
|
28
52
|
end
|
29
53
|
end
|
data/lib/telegram/bot/types.rb
CHANGED
@@ -10,3 +10,6 @@ require 'telegram/bot/types/contact'
|
|
10
10
|
require 'telegram/bot/types/location'
|
11
11
|
require 'telegram/bot/types/message'
|
12
12
|
require 'telegram/bot/types/update'
|
13
|
+
require 'telegram/bot/types/reply_keyboard_markup'
|
14
|
+
require 'telegram/bot/types/reply_keyboard_hide'
|
15
|
+
require 'telegram/bot/types/force_reply'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Telegram
|
2
|
+
module Bot
|
3
|
+
module Types
|
4
|
+
class ReplyKeyboardMarkup < Base
|
5
|
+
attribute :keyboard, Array[Array[String]]
|
6
|
+
attribute :resize_keyboard, Boolean, default: false
|
7
|
+
attribute :one_time_keyboard, Boolean, default: false
|
8
|
+
attribute :selective, Boolean, default: false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/telegram/bot/version.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.1
|
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-06-
|
11
|
+
date: 2015-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -131,10 +131,13 @@ files:
|
|
131
131
|
- lib/telegram/bot/types/base.rb
|
132
132
|
- lib/telegram/bot/types/contact.rb
|
133
133
|
- lib/telegram/bot/types/document.rb
|
134
|
+
- lib/telegram/bot/types/force_reply.rb
|
134
135
|
- lib/telegram/bot/types/group_chat.rb
|
135
136
|
- lib/telegram/bot/types/location.rb
|
136
137
|
- lib/telegram/bot/types/message.rb
|
137
138
|
- lib/telegram/bot/types/photo_size.rb
|
139
|
+
- lib/telegram/bot/types/reply_keyboard_hide.rb
|
140
|
+
- lib/telegram/bot/types/reply_keyboard_markup.rb
|
138
141
|
- lib/telegram/bot/types/sticker.rb
|
139
142
|
- lib/telegram/bot/types/update.rb
|
140
143
|
- lib/telegram/bot/types/user.rb
|