1:- use_module(library(http/thread_httpd)). 2:- use_module(library(http/http_dispatch)). 3:- use_module(library(http/http_error)). 4:- use_module(library(debug)). 5:- use_module(library(http/html_write)). 6:- use_module(library(http/http_parameters)). 7:- use_module(library(url)). 8:- use_module(library(sgml)). 9
11:- use_module(library(http/http_session)). 12
13:- use_module(interpret). 14
17
21server(Port) :-
22 http_server(http_dispatch, [port(Port)]).
23
25web_root(X) :- X = '.'. 27web_path(Relative,Absolute) :- web_root(Root), atom_concat(Root, Relative, Absolute).
35:- http_handler( '/pages' , serve_page, [prefix]). 36
37serve_page(Request) :-
38 memberchk(path(Path), Request),
39 web_path(Path, FilePath),
40 http_reply_file(FilePath, [], Request).
41
46:- http_handler(root(hello_world), say_hi, []). 49:- http_handler('/tacos/of/god' , say_tacos, []). 50
52:- http_handler('/aiml' , aiml_page , []). 53
55:- http_handler('/screenshot.png',
56 http_reply_file('screenshot.png', []), []). 57
58
66
67say_hi(_Request) :-
68 reply_html_page(title('Hello World'),
69 [ h1('Hello World'),
70 p(['This example demonstrates generating HTML ',
71 'messages from Prolog'
72 ]),
73 img([width(32),height(32),src('screenshot.png')])
74 ]).
75
76say_tacos(_Request) :-
77 debug(tacos , 'entered say_tacos', []),
78 format('Content-type: text/html~n~n'),
79 format('<html><body><h1>Tacos of God</h1></body></html').
80
81aiml_page(Request) :-
82 (http_session_data(last_utterance(Last_Utterance));
83 Last_Utterance = '') ,
84 (http_session_data(that(That)); That=[star(0)]),
85 (http_session_data(topic(Topic)); Topic=[star(0)]),
86 http_parameters(Request,
87 [intext(Intext , [ default('') ])]),
88 (http_session_retractall(last_utterance(_)) ; true),
89 http_session_assert(last_utterance(Intext)),
90 % TODO - need to use That, Topic, and Intext to find reply
91 % from chatterbot
92 chatterbot(memory(That, Topic), Intext, Response, NewTopic),
93 !, % cut in case something below fails - if there's an http problem
94 % I don't want to retry the bot
95 http_session_retractall(that(_)),
96 http_session_assert(that(Response)),
97 http_session_retractall(topic(_)),
98 http_session_assert(topic(NewTopic)),
99 reply_html_page(title('chatterbot'),
100 [ h1('Talk with the chatterbot'),
101 p(['I\'m a chatterbot, you can talk with me']),
102 p(['You said ', Last_Utterance]),
103 p(['and your last message was ', Intext]),
104 p(['and my response is ', Response]),
105 form([action=