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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3f04284d4adc0dc0ea36b79f436306d14e575a6
4
- data.tar.gz: b4188c3e144dca7c6e7d69228d475c606b8ca2c2
3
+ metadata.gz: b0d1012d03a804514e62c759b57bd498e3e788b7
4
+ data.tar.gz: 4a7b444e273ced43bba6b6d908adc21b8e226c0a
5
5
  SHA512:
6
- metadata.gz: 54eac803d445dd3c9a0d4168d182e2223c8b633c333489b872891807ee056b23bd856721b64f43ce2833aa44bd96edf6756954a8ba95ffbab4fa537ac49f090b
7
- data.tar.gz: 56e8965c98679340262261e7eb616c216380d049372d9f9568e8867dd5f688f838ce82b583f3698375134b7d09e3a9f7806defe91fd688a6681143ce93a1aa59
6
+ metadata.gz: b498e2c431bb5a01c777ae393f312e540c4b68d824cc02130eabeea1ad7a3f50e40a0ef58bef282e6145aaf685b5c48193a8cfac4866a6921746e715c941bf96
7
+ data.tar.gz: d2be7bb60aefda2f0a5f6dc3a1374975fbb824d1de79325da1622f2ce31f5519de65dbe0a9d914513d1c820519eb36dbaad225ba1054c80e4ffde4f40d257192
@@ -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
- @listen = {
24
- method: method,
25
- interval: interval,
26
- path: path
27
- }
28
- end
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 = nil
41
- case @listen[:method]
42
- when :poll
43
- listener = PollListener.new(self, @listen[:interval])
44
- when :webhook
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
@@ -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
- msg.type == :text and msg.text.start_with?("/#{@command}")
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
- msg.text.split[1..-1]
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.each do |update|
25
- @offset_id = update.id + 1
26
- message_received(update.message)
27
- end
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
@@ -1,3 +1,3 @@
1
1
  class TelegramBot
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
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.5
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-06 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client