Delphi 实现 图灵机器人API(IDHTTP POST )


本文整理自网络,侵删。

 
此功能所需的 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 使用资源文件

Delphi 自我复制源码

Delphi字符串中取数字

Delphi 声明指令

Delphi opendialog1文件过滤类型

Delphi firdac 对 sqlite 数字, int64也会被截断,会出现负数情况处理

tmsweb core 组件居中显示

Delphi windows 编程[1] - 窗体生成的过程一

Delphi 内存读取、修改

Delphi 解决android 9上无法使用http协议

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...