te_bot 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -2
- data/Gemfile.lock +4 -4
- data/README.md +76 -17
- data/lib/te_bot/cable.rb +3 -10
- data/lib/te_bot/court.rb +24 -12
- data/lib/te_bot/sender_options.rb +16 -16
- data/lib/te_bot/version.rb +1 -1
- data/lib/te_bot/wire.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6fa39962791458ebf9b8cd56bc4de36445e2fc5bbcdef2f360e12963cf970fc0
|
4
|
+
data.tar.gz: d774066a9e4d7a2608371de89444a16e8a9b7cd52842fa7e38bb9406b5f1a9f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d9afb6cb164d7811dd2459313f703e0c84b639b5194f521ae7938d2a0474af011fcd7ae1cc5054b009d67717a7cec0da0da4053f591fc9599c43120246933ee
|
7
|
+
data.tar.gz: 7006934fa7a9652e3acc61a37bd80de279a2662d36aee1f0638a433e7a64a2fa3140e93850ed58b1a3a59f2d413589409cef2b4512d0ce18acf693b4f2735752
|
data/CHANGELOG.md
CHANGED
@@ -12,8 +12,15 @@
|
|
12
12
|
- Change the implementation message replier from `reply(conn, message_string)` to `conn.reply text: message_string`.
|
13
13
|
- Add support for other message types such as audio, video, animation, markdown, and document
|
14
14
|
|
15
|
-
## [
|
15
|
+
## [Released]
|
16
16
|
|
17
17
|
## [0.3.0] - 2022-07-02
|
18
18
|
|
19
|
-
- Support for the handler to return response direcly by returning rack array or hash.
|
19
|
+
- Support for the handler to return response direcly by returning rack array or hash.
|
20
|
+
|
21
|
+
|
22
|
+
## [Unreleased]
|
23
|
+
|
24
|
+
## [0.3.1] - 2022-07-03
|
25
|
+
|
26
|
+
- Support for helpers with `helpers` macro
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
te_bot (0.
|
4
|
+
te_bot (0.4.0)
|
5
5
|
faraday (~> 2.3)
|
6
6
|
rack (~> 2.2)
|
7
7
|
|
@@ -14,10 +14,10 @@ GEM
|
|
14
14
|
byebug (11.1.3)
|
15
15
|
crack (0.4.5)
|
16
16
|
rexml
|
17
|
-
faraday (2.
|
18
|
-
faraday-net_http (
|
17
|
+
faraday (2.5.2)
|
18
|
+
faraday-net_http (>= 2.0, < 3.1)
|
19
19
|
ruby2_keywords (>= 0.0.4)
|
20
|
-
faraday-net_http (
|
20
|
+
faraday-net_http (3.0.0)
|
21
21
|
hashdiff (1.0.1)
|
22
22
|
minitest (5.16.1)
|
23
23
|
parallel (1.22.1)
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@ This gem is used to handle telegram webhook and sending message with telegram bo
|
|
10
10
|
|
11
11
|
Install the gem and add to the application's Gemfile
|
12
12
|
|
13
|
-
gem 'te_bot'
|
13
|
+
gem 'te_bot'
|
14
14
|
|
15
15
|
Then run
|
16
16
|
|
@@ -33,12 +33,12 @@ require "te_bot/sender_options"
|
|
33
33
|
class MyWebhookApp < TeBot::Court
|
34
34
|
access_token ENV["YOUR_BOT_ACCESS_TOKEN"]
|
35
35
|
|
36
|
-
command("/start") do
|
37
|
-
|
36
|
+
command("/start") do
|
37
|
+
reply text: "Welcome aboard my friend!"
|
38
38
|
end
|
39
39
|
|
40
|
-
command("/today") do
|
41
|
-
|
40
|
+
command("/today") do
|
41
|
+
reply text: Time.now.to_s
|
42
42
|
end
|
43
43
|
end
|
44
44
|
```
|
@@ -63,7 +63,7 @@ To run the app we can use rackup
|
|
63
63
|
For more detailed information about rack please visit [Rack Repository](https://github.com/rack/rack).
|
64
64
|
|
65
65
|
Now, our `MyWebhookApp` class is ready to handle some commands from telegram bot which are `/start` and `/today`.
|
66
|
-
The command aslo support argument that will be passed
|
66
|
+
The command aslo support argument that will be passed in the instance method `params`. To pass arguments, we can simply type `/today city:Jakarta limit:10`. The argument will be parsed as a Hash with string key => `{"city" => "Jakarta", "limit" => "10"}`.
|
67
67
|
|
68
68
|
To add a default handler for non existing command we can use the `#default_command` macro.
|
69
69
|
|
@@ -71,21 +71,23 @@ To add a default handler for non existing command we can use the `#default_comma
|
|
71
71
|
# app.rb
|
72
72
|
|
73
73
|
class MyWebhookApp < TeBot::Court
|
74
|
-
default_command do
|
75
|
-
|
74
|
+
default_command do
|
75
|
+
reply text: "Sorry, Comand not found. Try another command. or type /help"
|
76
76
|
end
|
77
77
|
end
|
78
78
|
```
|
79
79
|
|
80
|
-
|
80
|
+
All the messages are passed in the instance method `message`
|
81
|
+
|
82
|
+
Other type of messages are also supported by using this macros `text` for regular text message, `query`, `document`, `audio`, and `voice`. For more detail please check this [Telegram Docs](https://core.telegram.org/bots/webhooks#testing-your-bot-with-updates).
|
81
83
|
|
82
84
|
```rb
|
83
85
|
# app.rb
|
84
86
|
|
85
87
|
class MyWebhookApp < TeBot::Court
|
86
|
-
text do
|
87
|
-
|
88
|
-
|
88
|
+
text do
|
89
|
+
reply_message = do_some_fancy_stuff_here(message)
|
90
|
+
reply text: reply_message
|
89
91
|
end
|
90
92
|
end
|
91
93
|
```
|
@@ -95,8 +97,8 @@ And also we can define a macro for default action `#default_action` if the reque
|
|
95
97
|
# app.rb
|
96
98
|
|
97
99
|
class MyWebhookApp < TeBot::Court
|
98
|
-
default_action do
|
99
|
-
|
100
|
+
default_action do
|
101
|
+
reply text: "No, I can't talk like people. use command instead"
|
100
102
|
end
|
101
103
|
end
|
102
104
|
```
|
@@ -132,17 +134,74 @@ sender.send_message(chat_id, animation: { animation: url, caption: caption})
|
|
132
134
|
|
133
135
|
```
|
134
136
|
|
135
|
-
For markdown telegram supports
|
137
|
+
For markdown telegram supports MarkdownV2 [refer to this](https://core.telegram.org/bots/api#markdownv2-style)
|
136
138
|
Please check the [documentation](https://core.telegram.org/bots/api#sendmessage) for more details.
|
137
139
|
|
140
|
+
### Custom Handler
|
141
|
+
|
138
142
|
Of course you can add more handler by extending the `TeBot::Wire` class
|
139
143
|
|
140
144
|
```ruby
|
141
145
|
# in/your/custom_handler.rb
|
142
146
|
|
143
147
|
TeBot::Wire.class_eval do
|
144
|
-
sender :json do |
|
145
|
-
|
148
|
+
sender :json do |chat_id, message|
|
149
|
+
make_request("sendMessage", body: { chat_id: chat_id, json: message }.to_json)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
```
|
153
|
+
|
154
|
+
### Helpers
|
155
|
+
|
156
|
+
Using module
|
157
|
+
```ruby
|
158
|
+
# request_helpers.rb
|
159
|
+
|
160
|
+
module RequestHelpers
|
161
|
+
def get_nearest_hospitals
|
162
|
+
# do some fancy stuff
|
163
|
+
end
|
164
|
+
|
165
|
+
def get_the_nearest_routes(hospitals)
|
166
|
+
# do some even fancier stuff
|
167
|
+
end
|
168
|
+
|
169
|
+
def render_markdown(routes)
|
170
|
+
# do something else
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
class MyWebhookApp < TeBot::Court
|
175
|
+
include RequestHelpers
|
176
|
+
|
177
|
+
#... the rest of the code
|
178
|
+
end
|
179
|
+
|
180
|
+
```
|
181
|
+
|
182
|
+
Using instance methods
|
183
|
+
```ruby
|
184
|
+
|
185
|
+
# app.rb
|
186
|
+
|
187
|
+
class MyWebhookApp < TeBot::Court
|
188
|
+
command("/findhospital") do
|
189
|
+
hospitals = get_nearest_hospitals(params)
|
190
|
+
routes = get_the_nearest_routes(hospitals)
|
191
|
+
|
192
|
+
reply markdown: render_markdown(routes)
|
193
|
+
end
|
194
|
+
|
195
|
+
def get_nearest_hospitals(data)
|
196
|
+
# do some fancy stuff
|
197
|
+
end
|
198
|
+
|
199
|
+
def get_the_nearest_routes(hospitals)
|
200
|
+
# do some even fancier stuff
|
201
|
+
end
|
202
|
+
|
203
|
+
def render_markdown(routes)
|
204
|
+
# do something else
|
146
205
|
end
|
147
206
|
end
|
148
207
|
```
|
data/lib/te_bot/cable.rb
CHANGED
@@ -1,22 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module TeBot
|
4
|
-
|
5
|
-
attr_reader :message, :params
|
6
|
-
def initialize(wire, message, params = {})
|
7
|
-
@wire = wire
|
8
|
-
@message = message
|
9
|
-
@params = params
|
10
|
-
end
|
11
|
-
|
4
|
+
module Cable
|
12
5
|
def chat_id
|
13
|
-
|
6
|
+
message.data&.chat_id
|
14
7
|
end
|
15
8
|
|
16
9
|
def reply(**payload)
|
17
10
|
return if chat_id.nil?
|
18
11
|
|
19
|
-
|
12
|
+
wire.send_message chat_id, **payload
|
20
13
|
end
|
21
14
|
end
|
22
15
|
end
|
data/lib/te_bot/court.rb
CHANGED
@@ -5,6 +5,8 @@ require "json"
|
|
5
5
|
|
6
6
|
module TeBot
|
7
7
|
class Court
|
8
|
+
include TeBot::Cable
|
9
|
+
|
8
10
|
class << self
|
9
11
|
attr_reader :wire, :commands
|
10
12
|
|
@@ -43,6 +45,15 @@ module TeBot
|
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
48
|
+
attr_reader :params, :message, :wire
|
49
|
+
|
50
|
+
def initialize
|
51
|
+
@params = {}
|
52
|
+
@message = nil
|
53
|
+
@command = nil
|
54
|
+
@wire = self.class.wire
|
55
|
+
end
|
56
|
+
|
46
57
|
def call(env)
|
47
58
|
json_only(env) do |body|
|
48
59
|
response = handle_request(body)
|
@@ -76,33 +87,34 @@ module TeBot
|
|
76
87
|
end
|
77
88
|
|
78
89
|
def handle_request(body)
|
79
|
-
message = ::TeBot::Message.new(body)
|
90
|
+
@message = ::TeBot::Message.new(body)
|
91
|
+
|
92
|
+
@command, @params = @message.data&.content&.parse
|
80
93
|
|
81
|
-
|
82
|
-
conn = ::TeBot::Cable.new(self.class.wire, message, params || {})
|
94
|
+
@params = params
|
83
95
|
|
84
|
-
message.command do
|
85
|
-
handler = self.class.commands[command]
|
96
|
+
@message.command do
|
97
|
+
handler = self.class.commands[@command]
|
86
98
|
if handler.respond_to?(:call)
|
87
|
-
handler
|
99
|
+
instance_eval(&handler)
|
88
100
|
elsif self.class.default_command.respond_to?(:call)
|
89
|
-
self.class.default_command
|
101
|
+
instance_eval(&self.class.default_command)
|
90
102
|
end
|
91
103
|
end
|
92
104
|
|
93
105
|
::TeBot::Message::GENERAL_MESSAGE_TYPES.each do |message_type|
|
94
|
-
message.public_send(message_type) do
|
106
|
+
@message.public_send(message_type) do
|
95
107
|
handler = self.class.message_handlers(message_type)
|
96
108
|
|
97
109
|
next unless handler.respond_to?(:call)
|
98
|
-
handler
|
110
|
+
instance_eval(&handler)
|
99
111
|
end
|
100
112
|
end
|
101
113
|
|
102
|
-
if message.handler.respond_to?(:call)
|
103
|
-
message.
|
114
|
+
if @message.handler.respond_to?(:call)
|
115
|
+
instance_eval(&@message.handler)
|
104
116
|
elsif self.class.default_action.respond_to?(:call)
|
105
|
-
self.class.default_action
|
117
|
+
instance_eval(&self.class.default_action)
|
106
118
|
end
|
107
119
|
end
|
108
120
|
end
|
@@ -2,36 +2,36 @@ require_relative "./wire"
|
|
2
2
|
require "json"
|
3
3
|
|
4
4
|
TeBot::Wire.class_eval do
|
5
|
-
sender :text do |
|
6
|
-
|
5
|
+
sender :text do |chat_id, message|
|
6
|
+
make_request("sendMessage", body: {chat_id: chat_id, text: message}.to_json)
|
7
7
|
end
|
8
8
|
|
9
9
|
# this is using MarkdownV2 https://core.telegram.org/bots/api#markdownv2-style
|
10
|
-
sender :markdown do |
|
11
|
-
|
10
|
+
sender :markdown do |chat_id, message|
|
11
|
+
make_request("sendMessage", body: {chat_id: chat_id, text: message, parse_mode: "MarkdownV2"}.to_json)
|
12
12
|
end
|
13
13
|
|
14
|
-
sender :photo do |
|
15
|
-
|
14
|
+
sender :photo do |chat_id, message|
|
15
|
+
make_request("sendPhoto", body: message.merge({chat_id: chat_id}).to_json)
|
16
16
|
end
|
17
17
|
|
18
|
-
sender :audio do |
|
19
|
-
|
18
|
+
sender :audio do |chat_id, message|
|
19
|
+
make_request("sendAudio", body: message.merge({chat_id: chat_id}).to_json)
|
20
20
|
end
|
21
21
|
|
22
|
-
sender :document do |
|
23
|
-
|
22
|
+
sender :document do |chat_id, message|
|
23
|
+
make_request("sendDocument", body: message.merge({chat_id: chat_id}).to_json)
|
24
24
|
end
|
25
25
|
|
26
|
-
sender :video do |
|
27
|
-
|
26
|
+
sender :video do |chat_id, message|
|
27
|
+
make_request("sendVideo", body: message.merge({chat_id: chat_id}).to_json)
|
28
28
|
end
|
29
29
|
|
30
|
-
sender :animation do |
|
31
|
-
|
30
|
+
sender :animation do |chat_id, message|
|
31
|
+
make_request("sendAnimation", body: message.merge({chat_id: chat_id}).to_json)
|
32
32
|
end
|
33
33
|
|
34
|
-
sender :voice do |
|
35
|
-
|
34
|
+
sender :voice do |chat_id, message|
|
35
|
+
make_request("sendVoice", body: message.merge({chat_id: chat_id}).to_json)
|
36
36
|
end
|
37
37
|
end
|
data/lib/te_bot/version.rb
CHANGED
data/lib/te_bot/wire.rb
CHANGED
@@ -43,7 +43,7 @@ module TeBot
|
|
43
43
|
|
44
44
|
raise ArgumentError, "Message type invalid. sender :#{message_format} not defined" if handler.nil?
|
45
45
|
|
46
|
-
return
|
46
|
+
return instance_exec(chat_id, message, &handler) if handler.respond_to?(:call)
|
47
47
|
|
48
48
|
public_send(handler, chat_id, message)
|
49
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: te_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nawa Aripurna
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|