chatscript 0.0.5 → 0.0.7
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/chatscript.rb +141 -0
- data/lib/chatscript/version.rb +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35bb5bc0492859590235bcf85c7f0eb6b761141a
|
4
|
+
data.tar.gz: 2bdcf4d5c314dd2caeba541246d1dee025ccc9a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c39a000b72cba20a9ee4eb0f6a5226f8ac078738d4f9b95d4cd9fe5fd8a0730dc35db2e43526a2461c33c2ee242291aeaafbff60f0e491a5cabdd1d09b92f591
|
7
|
+
data.tar.gz: 9b403c429fbbdbef2b70fae2fa5efed7cd4c958111af3337524e484bdd6cdbbe7b43253e4eb901719a8162ffd9fae541ec136392d5018e45aa1ef8675c83a27a
|
data/lib/chatscript.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'socket'
|
3
|
+
require 'timeout'
|
4
|
+
require "chatscript/version"
|
5
|
+
|
6
|
+
module ChatScript
|
7
|
+
|
8
|
+
|
9
|
+
# https://www.chatbots.org/ai_zone/viewthread/2434/
|
10
|
+
class Message
|
11
|
+
attr_accessor :text, :oob
|
12
|
+
|
13
|
+
#
|
14
|
+
# msg = ChatScript::Message.new 'Bonjour a tout le monde', oob: 'salut()'
|
15
|
+
# msg.text
|
16
|
+
# => "Bonjour a tout le monde"
|
17
|
+
# msg.oob
|
18
|
+
# => "salut()"
|
19
|
+
#
|
20
|
+
# msg = ChatScript::Message.new '[ this is a oob message ] hi there! '
|
21
|
+
# msg.text
|
22
|
+
# => "hi there!"
|
23
|
+
# msg.oob
|
24
|
+
# => "this is a oob message"
|
25
|
+
# msg.to_str
|
26
|
+
# => "[ this is a oob message ] hi there!"
|
27
|
+
#
|
28
|
+
def initialize(text = '', oob: '')
|
29
|
+
if text.match(/\[.*\]/).nil?
|
30
|
+
# no oob inside txt
|
31
|
+
@text = text.strip
|
32
|
+
@oob = oob
|
33
|
+
else
|
34
|
+
# split oob from txt
|
35
|
+
@oob, @text = text.match(/\[(.*)\](.*)/).captures
|
36
|
+
|
37
|
+
# remove any blanks
|
38
|
+
@oob.strip!
|
39
|
+
@text.strip!
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def command?
|
44
|
+
@text.start_with? ':'
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_str
|
48
|
+
@oob.empty? ? @text : "[ #@oob ] #@text"
|
49
|
+
end
|
50
|
+
|
51
|
+
end # end class Message
|
52
|
+
|
53
|
+
|
54
|
+
class Client
|
55
|
+
|
56
|
+
def self.version
|
57
|
+
VERSION
|
58
|
+
end
|
59
|
+
|
60
|
+
attr_accessor :hostname, :port, :bot, :default_user
|
61
|
+
attr_reader :error
|
62
|
+
|
63
|
+
def initialize( hostname: 'localhost', port: 1024, bot: '',
|
64
|
+
default_user: 'guest', error_handler: method(:error_handler) )
|
65
|
+
@hostname = hostname
|
66
|
+
@port = port
|
67
|
+
@bot = bot
|
68
|
+
@default_user = default_user
|
69
|
+
@error_handler = error_handler
|
70
|
+
end
|
71
|
+
|
72
|
+
#
|
73
|
+
# error handler default method
|
74
|
+
# http://stackoverflow.com/questions/522720/passing-a-method-as-a-parameter-in-ruby
|
75
|
+
#
|
76
|
+
def error_handler
|
77
|
+
@error = $!
|
78
|
+
STDERR.print "#{timestamp}: runtime error connecting to server: #@error\n"
|
79
|
+
end
|
80
|
+
|
81
|
+
def last_error
|
82
|
+
@error
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# first volley
|
87
|
+
# user message is null (void string)
|
88
|
+
#
|
89
|
+
def start(user = @defaultuser)
|
90
|
+
volley(user, '')
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
#
|
95
|
+
# send a user message to the bot and get the bot reply
|
96
|
+
#
|
97
|
+
def volley( user = @default_user, message )
|
98
|
+
begin
|
99
|
+
# open client socket
|
100
|
+
socket = TCPSocket.open(@hostname, @port)
|
101
|
+
|
102
|
+
# send user message to bot
|
103
|
+
socket.write( "#{user}\0#{@bot}\0#{message}\0" )
|
104
|
+
|
105
|
+
# return the bot's answer as Message
|
106
|
+
Message.new socket.read
|
107
|
+
|
108
|
+
rescue
|
109
|
+
@error_handler.call
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# send a user message to the bot and get th ebot reply
|
115
|
+
#
|
116
|
+
def alive?
|
117
|
+
begin
|
118
|
+
# open client socket
|
119
|
+
socket = TCPSocket.open(@hostname, @port)
|
120
|
+
|
121
|
+
# send an alive check message
|
122
|
+
socket.write( "\0\0\0" )
|
123
|
+
|
124
|
+
Message.new socket.read
|
125
|
+
true
|
126
|
+
rescue
|
127
|
+
@error_handler.call
|
128
|
+
false
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
# return timestamp in ISO8601 with precision in milliseconds
|
135
|
+
def timestamp
|
136
|
+
Time.now.utc.iso8601(3)
|
137
|
+
end
|
138
|
+
|
139
|
+
end # end class
|
140
|
+
|
141
|
+
end # end module
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatscript
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Giorgio Robino
|
@@ -45,7 +45,9 @@ email:
|
|
45
45
|
executables: []
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
|
-
files:
|
48
|
+
files:
|
49
|
+
- lib/chatscript.rb
|
50
|
+
- lib/chatscript/version.rb
|
49
51
|
homepage: https://github.com/solyaris/rChatScript
|
50
52
|
licenses:
|
51
53
|
- MIT
|