1、图灵机器人概述
图灵机器人开放平台是北京光年无限科技旗下的个性化人工智能机器人开放平台。通过图灵机器人开放平台,软硬件产品开发者可快速为自己的产品接入一款具备个性化身份属性特征、满足不同场景多轮对话及上下文对话的人工智能机器人,实现产品的对话式交互。

2、图灵机器人接口描述
编码方式
UTF-8(调用图灵API的各个环节的编码方式均为UTF-8)

接口地址
http://openapi.tuling123.com/openapi/api/v2

请求方式
HTTP POST

请求参数
请求参数格式为 json

详细接入教程请见:https://www.kancloud.cn/turing/www-tuling123-com/718227
这里不再赘述。

3、mycode
此项目包含3个文件,main.c和tuling.c以及tuling.h,开发环境是基于VMware Workstation的Ubuntu系统,开发工具为gcc编译器,vim编辑器。

① tuling.h
包含使用的各种函数的声明,以及对话内容的封装申明。

#ifndef _TULING_H
#define _TULING_H
      
#define TR_CMD_MAX_LEN 1024
      
char cmd[TR_CMD_MAX_LEN] = "curl -l -H \"Content-type: application/json\" -X    POST -d '{\"reqType\": 0, \"perception\": {\"inputText\" : {\"text\": \"\"},    \"selfInfo\": {\"location\": {\"city\": \"成都\", \"province\": \"四川\",       \"street\": \"九康环路28号\"}}}, \"userInfo\": {\"apiKey\":                     \"84b4094632274f688bd24decddf2d8e2\", \"userId\": \"0\"}}' http://openapi.      tuling123.com/openapi/api/v2";
      
char* Insert_cmd(char *s1,char *cmd); 
char* Char_to_UTF8(const char *in,char *out);
char* Send_cmd(const char *cmd);
char* Print(const char *out,char *result);
#endif   

备注:文中char cmd[TR_CMD_MAX_LEN]中的84b4094632274f688bd24decddf2d8e2是图灵机器人的 apikey,可以通过自己注册后查看"机器人管理"中获得,亦可以使用本人的apikey(大家不嫌弃的话)

② tuling.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/*字符串转换为UTF8格式*/
char* Char_to_UTF8(const char *in, char *out)
{
    char temp[1024] = {0};
    int x = 0;
    unsigned long i;
    while(*in != '\0')
    {
        if(*in == '\\')
        {
            strcpy(temp,in);
            *temp = '0';
            *(temp + 4) = '\0';
            i = strtoul(temp, NULL, 16);
            out[x] = i;
            in += 3;
        }
        else
        {
            out[x] = *in;
        }
        x++;
        in++;
    }
    out[x] = '\0';
    return out;
}

/*将对话插入至cmd中*/
char *Insert_cmd(char *s1, char *s2)
{
    int len1 = 0, len2 = 0, j = 0, len3=110, k = 0,i=0;
    char s4[1024];
    char *s3 = s4;
    if (s1 == NULL)
    return NULL;
    if (s2 == NULL)
    return s1;
    len1 = strlen(s1);
    len2 = strlen(s2);
    if(len3>len1)
    return NULL;
    for (i = 0; i<len3; i++) 
    { 
        j++; 
    }
    for (i= 0; i<len1; i++)
    { 
        s4[k++] = s1[i];
    }

    for (i = 0; i<len2; i++)
    s1[j++] = s2[i];
    for(i=len3;i<len1;i++)
    s1[j++]=s4[i];
    s1[j] = '\0';
    return s1;
}

/*访问图灵机器人*/
char* Send_cmd(const char *cmd)
{
    static char buf[1024];
    memset(buf,0,sizeof(buf));
    FILE *p=popen(cmd,"r");
    if(p==NULL)
    {
        printf("fork failed\n");
        return NULL;
    }
    fgets(buf, sizeof(buf), p);
    return buf;
}

/*提取想要的文字*/
char* Print(const char *out,char *result) 
{
    char ch1[1024];
    memset(ch1,0,sizeof(ch1));
    strcpy(ch1,out);
    char *ch2;
    ch2=result;
    int i=0,j=0;
    while(ch1[i++]!='t');
    while(ch1[i++]!='e');
    while(ch1[i++]!='x');
    while(ch1[i++]!='t');
    while(ch1[i++]!='t');
    while(ch1[i++]!='e');
    while(ch1[i++]!='x');
    while(ch1[i++]!='t');
    i+=3;
    while(ch1[i])
    {
        ch2[j++]=ch1[i++];
    }
    ch2[j-5]='\0';
    return result;
}

③main.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include"tuling.h"

int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        printf("please enter the your question\n");
        return -1;
    }
    char out[TR_CMD_MAX_LEN];
    char *buf;
    char result[TR_CMD_MAX_LEN];
    memset(out,0,sizeof(out));
    memset(result,0,sizeof(result));
    Insert_cmd(cmd,argv[1]);
    Char_to_UTF8(cmd,out);
    buf=Send_cmd(out);
    Print(buf,result);
    printf("question:%s\nresult:%s\n",argv[1],result);
    return 0;
}

以下是效果图:

图灵机器人API 2.0的调用(基于C语言实现)

相关文章: