tgbot 0.1.6 → 0.2.2

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.
data/usage.md DELETED
@@ -1,97 +0,0 @@
1
- # Tgbot.Usage
2
-
3
- Since the implementation is so tiny, you can just review source
4
- code to understand usage.
5
-
6
- ## Example and Explanation
7
-
8
- ### Hello world
9
-
10
- ```ruby
11
- Tgbot.run TOKEN, proxy: 'https://127.0.0.1:1080' do |bot|
12
- bot.get 'hello' do reply 'world' end
13
- end
14
- ```
15
-
16
- This can be broken into: (with the same effect)
17
-
18
- ```ruby
19
- bot = Tgbot::DSL.new TOKEN, proxy: 'https://127.0.0.1:1080'
20
- bot.get 'hello' do reply 'world' end
21
- bot.run
22
- ```
23
-
24
- Here the "`bot`" is actually an instance of `Tgbot::DSL`.
25
-
26
- ### MainLoop Model
27
-
28
- Original Model:
29
-
30
- ```ruby
31
- loop { updates = get_updates; updates.each { |update| ... } }
32
- ```
33
-
34
- Only deal with `Update`:
35
-
36
- ```ruby
37
- loop_get_updates { |update| ... }
38
- ```
39
-
40
- Which is our `mainloop`: (see [runner.rb#L15](lib/tgbot/runner.rb#L15))
41
-
42
- ```ruby
43
- mainloop { |update| ... }
44
- ```
45
-
46
- ### DSL
47
-
48
- ```ruby
49
- bot.start { puts "#{bot.name}, at your service" }
50
- bot.finish { puts "byebye." }
51
- bot.before { |update| puts "Processing ##{update.id}." }
52
- bot.after { |update| puts "Processed ##{update.id}." }
53
- bot.on /\.r(\d+)?d(\d+)?/ do |matched|
54
- p self #=> #<Update id=123456789>
55
- t = matched[1]&.to_i || 1
56
- n = matched[2]&.to_i || 6
57
- reply _ = Array.new(t){rand n}.to_s rescue 'bad roll!'
58
- end
59
- bot.get 'cuxia', 'blind' do
60
- name = message&.from&.first_name
61
- next unless name
62
- send_message "#{name} cuxia!"
63
- if rand < 0.5
64
- self.retry 2 #=> at most retry 2 times, default 1 if not given arg
65
- end
66
- done! #=> prevent any retry, mark and drop
67
- end
68
- bot.alias 'cuxia', 'woc', 'wodemaya'
69
- ```
70
-
71
- - start: will be run once when `bot.run`.
72
- - finish: will be run once when <kbd>Ctrl</kbd><kbd>C</kbd>.
73
- - before: do something with every `Update` before processing.
74
- - after: do something with every `Update` after processing.
75
- - on/get: match text and execute code in `Update` instance.
76
-
77
-
78
-
79
- As you can see, the dsl is still weak. Wish for your idea!
80
-
81
- ### Call Bot API
82
-
83
- You can call bot API at any place with `bot.`.
84
-
85
- ```ruby
86
- p bot.get_me
87
- bot.get 'debug' do
88
- p bot.get_me
89
- end
90
- ```
91
-
92
- Params and returns are Hash.
93
-
94
- ### Upgrade Bot API
95
-
96
- - Edit [types.txt](tools/types.txt) or [methods.txt](tools/methods.txt).
97
- - `rake json`