• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

      此功能所需的 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知识库,但是如何活用知识库,扩充我们的机器人,实在是没啥好的方向,哪位小伙伴有兴趣可以指教下

     

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
遗传算法实例(Matlab实现)发布时间:2022-07-18
下一篇:
简单遗传算法MATLAB实现 - Alex Yu发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap