-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatscript.pl
72 lines (65 loc) · 1.86 KB
/
chatscript.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
:-module(chatscript, [talk/4,
start_conversation/3,
set_chatscript_address/1]).
/** <module> Connection to chatscript server
*
* http://chatscript.sourceforge.net/
*
* @author Anne Ogborn
* @license mit
* @version 1.0.0
*/
:- multifile license:license/3.
license:license(mit, lgpl,
[ comment('MIT License'),
url('http://opensource.org/licenses/MIT')
]).
:- license(mit).
:- use_module(library(http/http_client)).
:- use_module(library(http/http_open)).
:- use_module(library(http/http_header)).
:- dynamic server_address/1.
%% set_chatscript_address(+Address:term) is det
%
% @param Address is a term of form domain:port
%
% Set the address of the chatscript server
%
set_chatscript_address(Address) :-
retractall(server_address(_)),
asserta(server_address(Address)).
%% talk(+User:atom, +Bot:atom, +Message:atom, -Reply:string) is
% semidet
%
% Send a volley to the server
%
% @User User name
% @Bot name of the bot. The default bot is ''
% @Message user's input to the bot. No nl. Must not be the null
% string
% @Reply bots response
%
% You must call start_conversation before calling this
% for each user
%
talk(User, Bot, Message, Reply) :-
Message \= '',
talk_(User, Bot, Message, Reply).
talk_(User, Bot, Message, Reply) :-
format(string(S), '~w\x00~w\x00\~w\x00', [User, Bot, Message]),
server_address(Address),
tcp_connect(Address, StreamPair, []),
stream_pair(StreamPair, Read, Write),
write(Write, S),
flush_output(Write),
read_stream_to_codes(Read, Codes1),
delete(Codes1, 0, Codes2), % they insert nuls
string_codes(Reply, Codes2),
close(StreamPair).
%% start_conversation(+User:atom, +Bot:atom, -Reply:string) is det
%
% call this for each user to get initial response prior to calling
% talk/4
%
start_conversation(User, Bot, Reply) :-
talk_(User, Bot, '', Reply).