rikutelebot 0.0.7 → 0.0.8

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: 8892ad352c873f513b2588d71fe9b0f0c17a53af
4
- data.tar.gz: d820b5a330d778962a825338df10e5d40a101fe6
3
+ metadata.gz: 58ceab6f43a9ad90430bcb34a0a6dd64642c243d
4
+ data.tar.gz: 6a8fc7789a20af74bf18e3691039176aca42e877
5
5
  SHA512:
6
- metadata.gz: 9dd0ed4d1327577a6b3c3dbc54cbda31776c698884e431a5a678eec5b607bc07dd8013069bbdfe12cf3e061be262d3ec81fb009b30dd98f3ffbf6d874014f8eb
7
- data.tar.gz: d0e2bcdc3ba354bee1da8452b04d752055a7c7c2c46b75c968700a195771975c79180bfce139bd2569c63358930dd94646bd61ae423f1dbd556bfa8dad9643c6
6
+ metadata.gz: 01d4b98d9be68d842002f3215d5ea05d89d2ed684fce3169a4c8b0fa29218c9a1a5cd4d428925d7be6f399a78d6ab4f06ae6424dee1594eec9e42df82bc0f918
7
+ data.tar.gz: 6e6158136e3907b5eaa262aa524e2cb3de04e885660a89153214ffd4cdc95397e93fc43718b23cb8a74925927baeaea0ef97f05534cd3ce0881ef1ceed022d8b
@@ -1,3 +1,3 @@
1
1
  module Rikutelebot
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/rikutelebot.rb CHANGED
@@ -1,10 +1,14 @@
1
1
  require "rikutelebot/version"
2
2
  require 'open-uri'
3
3
  require 'json'
4
+ require 'monads/many'
5
+
6
+
4
7
 
5
8
  module Rikutelebot
9
+ include Monads
6
10
 
7
- Config = Struct.new(:token, :offset) do
11
+ Config = Struct.new(:token, :offset, :io) do
8
12
  def getupdatesurl
9
13
  "https://api.telegram.org/bot#{token}/getUpdates?offset=#{offset}"
10
14
  end
@@ -20,20 +24,47 @@ module Rikutelebot
20
24
  end
21
25
  end
22
26
 
23
- Command = Struct.new(:command, :args) do
27
+ Command = Struct.new(:command, :block)
24
28
 
25
- end
26
29
 
27
30
  class Bot
28
31
  def initialize
29
- @conf = Config.new nil, (File.exist?('offset') ? File.read('offset').to_i : 0)
32
+ @conf = Config.new
30
33
  @state = false
31
34
  @commands = []
32
35
  yield @conf
36
+ @conf.offset = @conf.io.get_key 'offset'
37
+ end
38
+
39
+ def on(command, &block)
40
+ @commands << Command.new(command, block)
41
+ self
33
42
  end
34
43
 
35
- def get_message_array
36
-
44
+ def get_command text
45
+ c = text.chars
46
+ return nil if c.first != '/'
47
+ c.shift
48
+ buf = ''
49
+ while c.first != ' ' and c.first != '@' and !c.empty?
50
+ buf += c.shift
51
+ end
52
+ return buf
53
+ end
54
+
55
+ def run!
56
+ loop do
57
+ on_message do |message|
58
+ a = get_command(message.text)
59
+ next if a.nil?
60
+ Many.new(
61
+ @commands.select do |cmd|
62
+ cmd.command == a
63
+ end
64
+ ).block.call(message).values
65
+ end
66
+ sleep 0.1
67
+ end
37
68
  end
38
69
 
39
70
  def on_message
@@ -41,26 +72,27 @@ module Rikutelebot
41
72
  j = JSON.parse open(@conf.getupdatesurl).read()
42
73
  return if j['result'].length == 0 or j.nil?
43
74
  for update in j['result']
44
- fn = update['message']['from']['first_name']
45
- ln = update['message']['from']['last_name']
46
- id = update['message']['from']['id']
47
- un = update['message']['from']['username']
75
+ begin
76
+ fn = update['message']['from']['first_name']
77
+ ln = update['message']['from']['last_name']
78
+ id = update['message']['from']['id']
79
+ un = update['message']['from']['username']
48
80
 
49
- usr = User.new(fn, ln, id, un)
81
+ usr = User.new(fn, ln, id, un)
50
82
 
51
- id = update['message']['chat']['id']
52
- title = update['message']['chat']['title']
53
- un = update['message']['chat']['username'] || "(#{id}:#{title})"
83
+ id = update['message']['chat']['id']
84
+ title = update['message']['chat']['title']
85
+ un = update['message']['chat']['username'] || "(#{id}:#{title})"
54
86
 
55
- chat = Chat.new(id, title, un)
87
+ chat = Chat.new(id, title, un)
56
88
 
57
- yield Message.new(usr, chat, update['message']['text']) unless update['message']['text'].nil?
89
+ yield Message.new(usr, chat, update['message']['text']) unless update['message']['text'].nil?
90
+ rescue
91
+ next
92
+ end
58
93
  end
59
94
  @conf.offset = j['result'].last['update_id'] + 1
60
- File.open('offset', 'w') do |f|
61
- f.write @conf.offset
62
- f.close
63
- end
95
+ @conf.io.set_key 'offset', @conf.offset.to_s
64
96
  end
65
97
 
66
98
  def commands
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rikutelebot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Riku
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-25 00:00:00.000000000 Z
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler