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 +4 -4
- data/lib/rikutelebot/version.rb +1 -1
- data/lib/rikutelebot.rb +52 -20
- 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: 58ceab6f43a9ad90430bcb34a0a6dd64642c243d
|
|
4
|
+
data.tar.gz: 6a8fc7789a20af74bf18e3691039176aca42e877
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 01d4b98d9be68d842002f3215d5ea05d89d2ed684fce3169a4c8b0fa29218c9a1a5cd4d428925d7be6f399a78d6ab4f06ae6424dee1594eec9e42df82bc0f918
|
|
7
|
+
data.tar.gz: 6e6158136e3907b5eaa262aa524e2cb3de04e885660a89153214ffd4cdc95397e93fc43718b23cb8a74925927baeaea0ef97f05534cd3ce0881ef1ceed022d8b
|
data/lib/rikutelebot/version.rb
CHANGED
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, :
|
|
27
|
+
Command = Struct.new(:command, :block)
|
|
24
28
|
|
|
25
|
-
end
|
|
26
29
|
|
|
27
30
|
class Bot
|
|
28
31
|
def initialize
|
|
29
|
-
@conf = Config.new
|
|
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
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
81
|
+
usr = User.new(fn, ln, id, un)
|
|
50
82
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
83
|
+
id = update['message']['chat']['id']
|
|
84
|
+
title = update['message']['chat']['title']
|
|
85
|
+
un = update['message']['chat']['username'] || "(#{id}:#{title})"
|
|
54
86
|
|
|
55
|
-
|
|
87
|
+
chat = Chat.new(id, title, un)
|
|
56
88
|
|
|
57
|
-
|
|
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
|
-
|
|
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.
|
|
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:
|
|
11
|
+
date: 2018-02-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|