tgbot 0.1.0 → 0.1.1

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: 662c96b10f41495f1a6aec7c80801950d22d40eb
4
- data.tar.gz: ce8833bebe2c18dc1976ef330460d64cacd52d23
3
+ metadata.gz: d47acdf7d9efc0bbc1046ec8da783f7275bf2f47
4
+ data.tar.gz: f5307a153d8b7d040ac8d89f9691ce0bb5d8bba4
5
5
  SHA512:
6
- metadata.gz: cba3af636ed854c1e225ae3cdbea48db5145ee3fc9422a3ff7431b60206c1e5ac3652bac64e59b2413184e871134d07df4d10a03b4acf66fe155e6b962c0af92
7
- data.tar.gz: 64129d237a5b93b5cb4e7a1c7aa56321cd4704df44c99b57fac81120965b548ece3defed49057e841aee1cfadfe05b6ada9853fcab90a3a638fcbd29968edb95
6
+ metadata.gz: 8c89444e9d5716ce16dbd3f4789558e060fb8653d1550d18489430f713341ad43010cec7a3f5d8d050c9e1e72e4640e107a5718ffade5d8541ff4c25457c066c
7
+ data.tar.gz: 3f25c02feffb114ef4f20e6efa2608fdebe23706df4a0903556679b433194e14e1eff9ff15e7aa600041e033540d67c40c0764fc5cb2e5268e8af75d19bf7eea
data/README.md CHANGED
@@ -1,42 +1,21 @@
1
1
  # `Tgbot`
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/tgbot.svg)](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
- It's still under experiment, not ready for use.
6
-
7
- ## `future.rb`
8
-
9
- ```ruby
10
- # `Tgbot.run' will start a loop until `Interrupt (Ctrl+C)' occured
11
- Tgbot.run TOKEN, proxy: 'https://127.0.0.1:1080' do |bot|
12
- bot.start do # once
13
- bot.ok "this is @#{bot.first_name}, sir."
14
- end
15
- bot.get 'drive' do # if message['drive']
16
- bot.send_photo garage.pop rescue bot.retry(1) { |x| bot.sorry "Failed #{x} times." }
17
- end # totally retry ^ times
18
- bot.on /\-md([.^]+)\Z/m do |matched|
19
- phantomjs 'md.js', matched
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
- @garage = load_data.shuffle
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 "this is \e[33m#{bot.name}\e[32m, master.", 2
12
+ log "\e[33m#{bot.name}\e[32m, at your service.", 2
11
13
  end
12
14
  bot.finish do
13
- log "byebye.", 1
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
- bot.get 'drive' do |x, update|
16
- pic = @garage.pop
17
- log ">> Sending #{File.basename(pic)} to @#{update.message.from.username} ##{update.id}", 6
18
- update.reply_photo pic, caption: File.basename(pic, '.*')
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} #{bot.timeout}"
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 @garage
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(regex, &blk)
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
- blk.call x, update if x
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
@@ -1,3 +1,3 @@
1
1
  module Tgbot
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
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.0
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-07-30 00:00:00.000000000 Z
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler