telegram_bot_ruby 0.1.5 → 0.1.6
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/lib/telegram_bot.rb +12 -17
- data/lib/telegram_bot/matcher.rb +21 -3
- data/lib/telegram_bot/poll_listener.rb +24 -5
- data/lib/telegram_bot/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: b0d1012d03a804514e62c759b57bd498e3e788b7
|
4
|
+
data.tar.gz: 4a7b444e273ced43bba6b6d908adc21b8e226c0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b498e2c431bb5a01c777ae393f312e540c4b68d824cc02130eabeea1ad7a3f50e40a0ef58bef282e6145aaf685b5c48193a8cfac4866a6921746e715c941bf96
|
7
|
+
data.tar.gz: d2be7bb60aefda2f0a5f6dc3a1374975fbb824d1de79325da1622f2ce31f5519de65dbe0a9d914513d1c820519eb36dbaad225ba1054c80e4ffde4f40d257192
|
data/lib/telegram_bot.rb
CHANGED
@@ -17,18 +17,16 @@ class TelegramBot
|
|
17
17
|
def initialize(history: 50)
|
18
18
|
@history = []
|
19
19
|
@history_length = 50
|
20
|
+
@listener = nil
|
20
21
|
end
|
21
22
|
|
22
23
|
def listen(method: :poll, interval: 5, path: '/hook')
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
def extend_env(env)
|
31
|
-
|
24
|
+
case method
|
25
|
+
when :poll
|
26
|
+
@listener = PollListener.new(self, interval)
|
27
|
+
when :webhook
|
28
|
+
warn 'not implemented'
|
29
|
+
end
|
32
30
|
end
|
33
31
|
|
34
32
|
def append_history(message)
|
@@ -37,13 +35,10 @@ class TelegramBot
|
|
37
35
|
end
|
38
36
|
|
39
37
|
def start!
|
40
|
-
listener
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
warn 'not implemented'
|
46
|
-
end
|
47
|
-
listener.start!
|
38
|
+
@listener.start!
|
39
|
+
end
|
40
|
+
|
41
|
+
def stop!
|
42
|
+
@listener.stop!
|
48
43
|
end
|
49
44
|
end
|
data/lib/telegram_bot/matcher.rb
CHANGED
@@ -35,16 +35,34 @@ class TelegramBot
|
|
35
35
|
class CommandMatcher < Matcher
|
36
36
|
attr_accessor :command
|
37
37
|
|
38
|
-
def initialize(command)
|
38
|
+
def initialize(command = nil, no_split: false)
|
39
39
|
@command = command
|
40
|
+
@no_split = no_split
|
40
41
|
end
|
41
42
|
|
42
43
|
def ===(msg)
|
43
|
-
|
44
|
+
start_with = '/'
|
45
|
+
if !@command.nil?
|
46
|
+
start_with += @command.to_s
|
47
|
+
end
|
48
|
+
return false if msg.type != :text
|
49
|
+
return false if !msg.text.start_with? start_with
|
50
|
+
|
51
|
+
true
|
44
52
|
end
|
45
53
|
|
46
54
|
def arguments(msg)
|
47
|
-
|
55
|
+
case
|
56
|
+
when @no_split
|
57
|
+
cmd, _, args = msg.text.parition(/\s/)
|
58
|
+
[cmd[1..-1], args]
|
59
|
+
when @comamnd.nil?
|
60
|
+
cmd, *args = msg.text.split
|
61
|
+
[cmd[1..-1], *args]
|
62
|
+
else
|
63
|
+
cmd, *args = msg.text.split
|
64
|
+
args
|
65
|
+
end
|
48
66
|
end
|
49
67
|
end
|
50
68
|
|
@@ -4,6 +4,7 @@ class TelegramBot
|
|
4
4
|
@client = client
|
5
5
|
@interval = interval
|
6
6
|
@offset_id = nil
|
7
|
+
@terminate_signal = false
|
7
8
|
end
|
8
9
|
|
9
10
|
def message_received(msg)
|
@@ -11,9 +12,26 @@ class TelegramBot
|
|
11
12
|
@client.handle(msg)
|
12
13
|
end
|
13
14
|
|
15
|
+
def stop!
|
16
|
+
@terminate_signal = true
|
17
|
+
end
|
18
|
+
|
14
19
|
def start!
|
20
|
+
@terminate_signal = false
|
21
|
+
|
15
22
|
loop do
|
16
|
-
get_updates
|
23
|
+
get_updates.each do |update|
|
24
|
+
message_received(update.message)
|
25
|
+
@offset_id = update.id + 1
|
26
|
+
|
27
|
+
if @terminate_signal
|
28
|
+
save_offset
|
29
|
+
break
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
break if @terminate_signal
|
34
|
+
|
17
35
|
sleep @interval
|
18
36
|
end
|
19
37
|
end
|
@@ -21,10 +39,11 @@ class TelegramBot
|
|
21
39
|
def get_updates
|
22
40
|
updates = @client.get_updates(offset: @offset_id,
|
23
41
|
limit: 50)
|
24
|
-
updates
|
25
|
-
|
26
|
-
|
27
|
-
|
42
|
+
updates
|
43
|
+
end
|
44
|
+
|
45
|
+
def save_offset
|
46
|
+
@client.get_updates(limit: 0, offset: @offset_id)
|
28
47
|
end
|
29
48
|
end
|
30
49
|
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.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shou Ya
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|