elizaweb.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. program elizaweb;
  2. {$ifdef fpc}
  3. {$mode delphi}{$H+}
  4. {$endif}
  5. uses
  6. {$IFDEF UNIX}{$IFDEF UseCThreads}
  7. cthreads,
  8. {$ENDIF}{$ENDIF}
  9. Classes,
  10. ezBillClinton,
  11. ezPersonality,
  12. ezEliza,
  13. ezEngine,
  14. ezMSTechSupport,
  15. IdBaseComponent,
  16. IdComponent,
  17. IdTCPServer,
  18. IdCustomHTTPServer,
  19. IdHTTPServer, IdContext, IdCustomTCPServer, IdSocketHandle, SysUtils;
  20. type
  21. TElizaWebProg = class(TObject)
  22. protected
  23. IdHTTPServer1: TIdHTTPServer;
  24. FHTMLDir: string;
  25. FTemplate: string;
  26. //
  27. procedure Ask(ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  28. procedure IdHTTPServer1SessionStart(Sender: TIdHTTPSession);
  29. procedure IdHTTPServer1SessionEnd(Sender: TIdHTTPSession);
  30. procedure IdHTTPServer1CommandGet(AContext: TIdContext;
  31. ARequestInfo: TIdHTTPRequestInfo;
  32. AResponseInfo: TIdHTTPResponseInfo);
  33. public
  34. constructor Create;
  35. destructor Destroy; override;
  36. end;
  37. constructor TElizaWebProg.Create;
  38. var b : TIdSocketHandle;
  39. begin
  40. inherited Create;
  41. idhttpserver1 := TIdHTTPServer.Create;
  42. b:=idhttpserver1.Bindings.Add;
  43. b.IP:='127.0.0.1';
  44. b.port:=8000;
  45. idhttpserver1.DefaultPort := 25000;
  46. idhttpserver1.AutoStartSession := True;
  47. idhttpserver1.ServerSoftware := 'Eliza Web';
  48. idhttpserver1.SessionTimeOut := 600000;
  49. idhttpserver1.OnSessionStart := IdHTTPServer1SessionStart;
  50. idhttpserver1.OnSessionEnd := IdHTTPServer1SessionEnd;
  51. idhttpserver1.OnCommandGet := IdHTTPServer1CommandGet;
  52. idhttpserver1.SessionState := True;
  53. idhttpserver1.active:=true;
  54. FHTMLDir := ExtractFilePath(ParamStr(0)) + 'HTML';
  55. with TFileStream.Create(includetrailingpathdelimiter(FHTMLDir)+ 'eliza.html', fmOpenRead) do try
  56. SetLength(FTemplate, Size);
  57. ReadBuffer(FTemplate[1], Size);
  58. finally Free; end;
  59. end;
  60. destructor TElizaWebProg.Destroy;
  61. begin
  62. FreeAndNil(idhttpserver1);
  63. inherited Destroy;
  64. end;
  65. procedure TElizaWebProg.IdHTTPServer1SessionStart(Sender: TIdHTTPSession);
  66. begin
  67. Sender.Content.AddObject('Eliza', TEZEngine.Create(nil));
  68. end;
  69. procedure TElizaWebProg.IdHTTPServer1SessionEnd(Sender: TIdHTTPSession);
  70. begin
  71. TEZEngine(Sender.Content.Objects[0]).Free;
  72. end;
  73. procedure TElizaWebProg.IdHTTPServer1CommandGet(AContext: TIdContext;
  74. ARequestInfo: TIdHTTPRequestInfo;
  75. AResponseInfo: TIdHTTPResponseInfo);
  76. var
  77. LFilename: string;
  78. LPathname: string;
  79. begin
  80. LFilename := ARequestInfo.Document;
  81. if AnsiSameText(LFilename, '/eliza.html') then begin
  82. Ask(ARequestInfo, AResponseInfo);
  83. end else begin
  84. if LFilename = '/' then begin
  85. LFilename := '/index.html';
  86. end;
  87. LPathname := FHTMLDir + LFilename;
  88. if FileExists(LPathname) then begin
  89. AResponseInfo.ContentStream := TFileStream.Create(LPathname, fmOpenRead + fmShareDenyWrite);
  90. end else begin
  91. AResponseInfo.ResponseNo := 404;
  92. AResponseInfo.ContentText := 'The requested URL ' + ARequestInfo.Document
  93. + ' was not found on this server.';
  94. end;
  95. end;
  96. end;
  97. procedure TElizaWebProg.Ask(ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  98. var
  99. s: string;
  100. LEliza: TEZEngine;
  101. LPersonality: string;
  102. LResponse: string;
  103. LSound: string;
  104. LQuestion: string;
  105. begin
  106. LResponse := '';
  107. LEliza := TEZEngine(ARequestInfo.Session.Content.Objects[0]);
  108. LPersonality := Trim(ARequestInfo.Params.Values['Personality']);
  109. if LPersonality <> '' then begin
  110. LEliza.SetPersonality(LPersonality);
  111. end else begin
  112. LQuestion := Trim(ARequestInfo.Params.Values['Thought']);
  113. if LQuestion <> '' then begin
  114. LResponse := LEliza.TalkTo(LQuestion, LSound);
  115. end;
  116. end;
  117. if LEliza.Done then begin
  118. AResponseInfo.ContentText := LResponse;
  119. end else begin
  120. s := FTemplate;
  121. s := StringReplace(s, '{%RESPONSE%}', LResponse, []);
  122. if LSound <> '' then begin
  123. // I cannot distibute the wav files, they are from a commercial game, but I use
  124. // them when showing the demo live.
  125. if FileExists(FHTMLDir + '\' + LSound) then begin
  126. LSound := '<BGSOUND SRC=' + LSound + '.wav>';
  127. end else begin
  128. LSound := '';
  129. end;
  130. end;
  131. s := StringReplace(s, '{%SOUND%}', LSound, []);
  132. AResponseInfo.ContentText := s;
  133. end;
  134. end;
  135. var GProg : TElizaWebProg;
  136. begin
  137. GProg := TElizaWebProg.Create;
  138. try
  139. WriteLn('Eliza Demo now available at:');
  140. WriteLn('');
  141. WriteLn('http://127.0.0.1:8000/');
  142. WriteLn('');
  143. WriteLn('Press enter when finished');
  144. ReadLn;
  145. finally
  146. FreeAndNil(GProg);
  147. end;
  148. end.