本文整理自网络,侵删。
此功能所需的 Key及接口文档,都可以在图灵机器人的官网下载, 小伙伴们需要申请自己的图灵机器人账号。
申请方法请自行百度“图灵机器人” 。
登录账号后,在左侧的[机器人接入],获取需要的信息,记得一定要关闭 secret,开启的话,需要对请求进行特殊处理,具体处理方法可以看接口文档中的“数据加密Demo”,当然Java 开发的小伙伴可以直接使用Demo(流行的语言真好,东西都是现成的)

下面贴出的是POST请求,实现图灵机器人的方法。
unit Demo;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,IdHTTP;
type TForm1 = class(TForm) Memo1: TMemo; Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } function SendMsg(Msg : string) : string; end;
var Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
function TForm1.SendMsg(Msg : string) : string;var idhttp : TIdHTTP; url,ResquestStr,ResponseStr : string; ResquestStream,ResponseStream : TStringStream;begin Result := ''; idhttp := TIdHTTP.Create(nil); idhttp.Request.ContentType := '';
//info 传递信息需要 UTF8 加密,否则机器人不能正确识别 ResquestStr := '{"key":"你的KEY","info":"'+ UTF8Encode(Msg) +'","userid":"demo1"}';
//将传递的信息,写入请求流 ResquestStream := TStringStream.Create(ResquestStr); ResponseStream := TStringStream.Create(''); url := 'http://www.tuling123.com/openapi/api'; try try //发起请求 idhttp.Post(url,ResquestStream,ResponseStream); except on e: Exception do begin ShowMessage('出现异常:' + e.Message); end; end; //获取响应的信息 ResponseStr := ResponseStream.DataString; //响应的信息需要进行 UTF8 解密 ResponseStr := UTF8Decode(ResponseStr); Result := ResponseStr; finally idhttp.Free; ResquestStream.Free; ResponseStream.Free; end;end;
procedure TForm1.Button1Click(Sender: TObject);var acceptInfo : string;begin //向图灵机器人发送信息,并获取返回 acceptInfo := SendMsg(Edit1.Text); //将信息在界面上显示 Memo1.Lines.Add(acceptInfo);end;
end.复制代码大概的方法就是这样了
题外话:虽然实现了图灵机器人API,图灵机器人有自己的NLP知识库,但是如何活用知识库,扩充我们的机器人,实在是没啥好的方向,哪位小伙伴有兴趣可以指教下
相关阅读 >>
Delphi firedac 下的 sqlite [8] - 自定义函数
Delphi中从windows资源管理器中拖放图片到timage控件上
Delphi android实例-红外线操作(xe10.2+小米5)
Delphi 解决“richedit line insertion error”错误
Delphi ifileoperation替换shfileoperation
更多相关阅读请进入《Delphi》频道 >>