如何将 Dialogflow v2 与 angular 7 集成 [英] How to integrate Dialogflow v2 with angular 7

查看:39
本文介绍了如何将 Dialogflow v2 与 angular 7 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Dialogflow 构建聊天机器人,并使用内联编辑器将云功能部署到 firebase.

I am building a chatbot using Dialogflow, and I deployed the cloud function using inline editor to firebase.

但是,与 V1 相比,没有与 API 交互的 javascript SDK.我被卡住了,我在文档中找不到任何有用的东西.

However, compared to V1 there is no javascript SDK to interact with the API. I'm stuck and I cannot find anything helpful in the docs.

您能否分享一些有关如何使用 angular ou any j 框架与 dialogflow API 交互的示例代码?

Can you please share some example code on how to interact with the dialogflow API using angular ou any j framework ?

谢谢!

我尝试阅读 dialogflow 文档,尤其是有关 http 请求的部分.我用简单的 http post 请求尝试了一些代码,但没有用.

I tried to read the dialogflow doc especially the part about http requests. I tried some code with simple http post request which didn't work.

没有关于stackoverflow的具体例子.

There is no concrete example on stackoverflow.

推荐答案

您可以使用 Dialogflow Rest API,您需要使用 Google 云 sdk 生成访问令牌(范围:云平台、对话流)

You can use Dialogflow Rest APIs, You need to generate access token with Google cloud sdk (scope: cloud platform, dialogflow)

  public df_client_call(request) {
    var config = {
      headers: {
        'Authorization': "Bearer " + this.accessToken,
        'Content-Type': 'application/json; charset=utf-8'
      }
    };   
   return this.http.post(
      'https://dialogflow.googleapis.com/v2/projects/' + environment.project_id +
      '/agent/sessions/' + sessionId + ':detectIntent',
      request,
      config
    )
  }

request中你必须通过,

{
    queryInput: {
        text: {
            text: action.payload.text,
            languageCode: 'en-US',
        },
    }
}

sessionId => 用户的唯一 ID

sessionId => unique Id for your user

const googleAuth = require('google-oauth-jwt');

getToken: async function() {
    return new Promise((resolve) => {
        googleAuth.authenticate(
            {
                email: config.googleClientEmail,
                key: config.googlePrivateKey,
                scopes: ['https://www.googleapis.com/auth/cloud-platform'],
            },
            (err, token) => {
                resolve(token);
            },
        );
    });
},

这篇关于如何将 Dialogflow v2 与 angular 7 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆