tgbot 0.1.0 → 0.1.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 +17 -38
- data/example.rb +86 -10
- data/lib/tgbot/dsl.rb +7 -4
- data/lib/tgbot/update.rb +7 -0
- data/lib/tgbot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d47acdf7d9efc0bbc1046ec8da783f7275bf2f47
|
4
|
+
data.tar.gz: f5307a153d8b7d040ac8d89f9691ce0bb5d8bba4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c89444e9d5716ce16dbd3f4789558e060fb8653d1550d18489430f713341ad43010cec7a3f5d8d050c9e1e72e4640e107a5718ffade5d8541ff4c25457c066c
|
7
|
+
data.tar.gz: 3f25c02feffb114ef4f20e6efa2608fdebe23706df4a0903556679b433194e14e1eff9ff15e7aa600041e033540d67c40c0764fc5cb2e5268e8af75d19bf7eea
|
data/README.md
CHANGED
@@ -1,42 +1,21 @@
|
|
1
1
|
# `Tgbot`
|
2
2
|
|
3
|
+
[](https://badge.fury.io/rb/tgbot)
|
4
|
+
|
3
5
|
A tiny but easy-to-use wrapper of [Telegram Bot API](https://core.telegram.org/bots/api).
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
bot.send_photo 'cp.jpg' rescue bot.sorry
|
21
|
-
end
|
22
|
-
bot.finish do # rescue Interrupt
|
23
|
-
bot.ok 'byebye.'
|
24
|
-
end
|
25
|
-
bot.before_update do |update|
|
26
|
-
# do something before handling every [update]
|
27
|
-
end
|
28
|
-
bot.after_update do |update|
|
29
|
-
# do something after handling every [update]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
```
|
33
|
-
|
34
|
-
## Further future
|
35
|
-
|
36
|
-
Features often needed by bots.
|
37
|
-
|
38
|
-
- database
|
39
|
-
- session
|
40
|
-
- access control
|
41
|
-
- dynamically add functions
|
42
|
-
-
|
7
|
+
## Install
|
8
|
+
|
9
|
+
gem install tgbot
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
See [example.rb](example.rb).
|
14
|
+
|
15
|
+
## Todo
|
16
|
+
|
17
|
+
- improve DSL
|
18
|
+
|
19
|
+
## Contribute
|
20
|
+
|
21
|
+
PRs/issues are welcome.
|
data/example.rb
CHANGED
@@ -1,24 +1,74 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'faraday'
|
1
3
|
require './helper'
|
2
4
|
save_pid
|
3
5
|
require 'tgbot'
|
4
|
-
|
5
|
-
|
6
|
+
Garage = load_data.shuffle
|
7
|
+
Cache = {}
|
6
8
|
TOKEN =
|
7
9
|
Tgbot.run TOKEN, proxy: 'https://127.0.0.1:1080' do |bot|
|
8
10
|
|
9
11
|
bot.start do
|
10
|
-
log "
|
12
|
+
log "\e[33m#{bot.name}\e[32m, at your service.", 2
|
11
13
|
end
|
12
14
|
bot.finish do
|
13
|
-
log "byebye.",
|
15
|
+
log "byebye.", 2
|
16
|
+
end
|
17
|
+
|
18
|
+
bot.get 'start' do
|
19
|
+
send_message(<<~EOF, parse_mode: 'Markdown')
|
20
|
+
```
|
21
|
+
start : 显示此帮助信息
|
22
|
+
drive : 随机返回一张车库里的图
|
23
|
+
对该图回复 “原图” : 返回原图
|
24
|
+
exchange 100 CNY to JPY : 汇率转换
|
25
|
+
register : 添加自定义功能(会先提交给作者)
|
26
|
+
```
|
27
|
+
EOF
|
14
28
|
end
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
29
|
+
|
30
|
+
bot.get 'drive' do
|
31
|
+
pic = Garage.pop
|
32
|
+
log ">> Sending #{File.basename(pic)} to @#{message.from.username} ##{id}", 6
|
33
|
+
bytes = File.size pic
|
34
|
+
size = hsize bytes
|
35
|
+
reply "正在发车 (#{size} #{htime(bytes / 30720)})"
|
36
|
+
x = reply_photo pic, caption: File.basename(pic, '.*')
|
37
|
+
if self.done = x['ok']
|
38
|
+
Cache["drive_#{x['result']['message_id']}"] = pic
|
39
|
+
end
|
40
|
+
self.done! if self.count > 1
|
41
|
+
end
|
42
|
+
bot.get '原图' do
|
43
|
+
x = message&.reply_to_message&.message_id
|
44
|
+
pic = Cache["drive_#{x}"]
|
45
|
+
unless pic
|
46
|
+
reply '没找到原图,重开'
|
47
|
+
next
|
48
|
+
end
|
49
|
+
log ">> Sending original #{File.basename(pic)} to @#{message.from.username} ##{id}", 6
|
50
|
+
reply_document pic
|
51
|
+
end
|
52
|
+
|
53
|
+
bot.get 'exchange' do
|
54
|
+
x = text&.match /([-+]?[1-9]\d*(\.\d+)?)\s*([A-Z]+)\s*to\s*([A-Z]+)/
|
55
|
+
unless x
|
56
|
+
reply 'Usage: exchange 100 CNY to JPY'
|
57
|
+
next
|
58
|
+
end
|
59
|
+
n, f, t = x.values_at 1, 3, 4
|
60
|
+
n = Float(n) rescue next
|
61
|
+
Cache["exchange_#{f}"] ||= JSON.parse Faraday.get("http://api.fixer.io/latest?base=#{f}").body
|
62
|
+
next unless Cache["exchange_#{f}"] && !Cache["exchange_#{f}"]['error']
|
63
|
+
next unless Cache["exchange_#{f}"]['rates'][t]
|
64
|
+
n *= Cache["exchange_#{f}"]['rates'][t]
|
65
|
+
t = Cache["exchange_#{f}"]['date']
|
66
|
+
reply "#{'%.2f' % n} (#{t})"
|
19
67
|
end
|
68
|
+
|
20
69
|
bot.before do |update|
|
21
|
-
log ">> Processing ##{update.id}
|
70
|
+
log ">> Processing ##{update.id}"
|
71
|
+
log "@#{update.message&.from.username}: #{update.text}", 3
|
22
72
|
end
|
23
73
|
bot.after do |update|
|
24
74
|
if update.done?
|
@@ -28,7 +78,33 @@ Tgbot.run TOKEN, proxy: 'https://127.0.0.1:1080' do |bot|
|
|
28
78
|
end
|
29
79
|
end
|
30
80
|
|
81
|
+
bot.get 'register' do
|
82
|
+
e = message&.entities&.find { |e| e.type == 'pre' }
|
83
|
+
if e.nil?
|
84
|
+
send_message(<<~EOF)
|
85
|
+
register <功能名>
|
86
|
+
```
|
87
|
+
get /command/ do |matched|
|
88
|
+
# your code here
|
89
|
+
end
|
90
|
+
```
|
91
|
+
EOF
|
92
|
+
next
|
93
|
+
end
|
94
|
+
open 'register.rb', 'a' do |f|
|
95
|
+
f.puts text[e.offset, e.length]
|
96
|
+
end
|
97
|
+
reply '脚本已备分'
|
98
|
+
end
|
99
|
+
|
100
|
+
bot.get 'coin' do
|
101
|
+
send_message Array.new(text&.match(/\d+/)&.to_s.to_i || 1){ ['🌞', '🌚'].sample }.join
|
102
|
+
end
|
103
|
+
bot.get 'roll' do
|
104
|
+
send_message rand(text&.match(/\d+/)&.to_s.to_i || 100).to_s
|
105
|
+
end
|
106
|
+
|
31
107
|
end
|
32
108
|
|
33
|
-
save_data
|
109
|
+
save_data Garage
|
34
110
|
delete_pid
|
data/lib/tgbot/dsl.rb
CHANGED
@@ -19,10 +19,13 @@ module Tgbot
|
|
19
19
|
def after(&blk)
|
20
20
|
@procs[:after] = blk
|
21
21
|
end
|
22
|
-
def on(
|
23
|
-
@procs[:command][regex] = blk
|
22
|
+
def on(*regexes, &blk)
|
23
|
+
regexes.each { |regex| @procs[:command][regex] = blk }
|
24
24
|
end
|
25
25
|
alias get on
|
26
|
+
def alias(ori, *args)
|
27
|
+
args.each { |regex| @procs[:command][regex] = @procs[:command][ori] }
|
28
|
+
end
|
26
29
|
def run
|
27
30
|
yield self if block_given?
|
28
31
|
@procs[:start]&.call
|
@@ -32,14 +35,14 @@ module Tgbot
|
|
32
35
|
update.done = true
|
33
36
|
@procs[:command].each do |key, blk|
|
34
37
|
x = update.text&.match key
|
35
|
-
|
38
|
+
update.instance_exec(x, &blk) if x
|
36
39
|
end
|
37
40
|
@procs[:after]&.call update
|
38
41
|
end
|
39
42
|
rescue Interrupt
|
40
43
|
@procs[:finish]&.call
|
41
44
|
rescue => e
|
42
|
-
puts
|
45
|
+
puts e
|
43
46
|
puts e.backtrace
|
44
47
|
retry
|
45
48
|
end
|
data/lib/tgbot/update.rb
CHANGED
@@ -19,6 +19,12 @@ module Tgbot
|
|
19
19
|
def text
|
20
20
|
@update[@type].text
|
21
21
|
end
|
22
|
+
def done!
|
23
|
+
@done = true
|
24
|
+
end
|
25
|
+
def retry n = 1
|
26
|
+
@done = false if @count < n
|
27
|
+
end
|
22
28
|
def send_message(text = nil, **kwargs)
|
23
29
|
return unless chat_id
|
24
30
|
return unless text = text || kwargs.delete(:text)
|
@@ -31,6 +37,7 @@ module Tgbot
|
|
31
37
|
chat_id: chat_id, text: text,
|
32
38
|
reply_to_message_id: @update[@type].message_id, **kwargs)
|
33
39
|
end
|
40
|
+
alias reply reply_message
|
34
41
|
%i(photo audio document video voice video_note).each do |name|
|
35
42
|
class_eval %{
|
36
43
|
def send_#{name}(#{name} = nil, **kwargs)
|
data/lib/tgbot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tgbot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hyrious
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|