How to access Dialogflow V2 API from a webpage?

烂漫一生 提交于 2020-05-25 09:31:13

问题


I have a webpage where I want to use dialogflow chatbot. This is a custom chat window, so I don't want to use one click integration. I am able to access the chat agent V1 API using javascript/ajax (by passing client access token in the request header).

But I don't know how to do it in V2 API. The dialogflow documentation is not clear to me(I have setup Authentication by referring this link. I don't know how to proceed further). I'm not familiar with Google cloud either. So a working sample or a step by step how to access the API guideline will be very much appreciated.


回答1:


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
    )
  }

In the request you have to pass,

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

to trigger Event:,

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

sessionId => unique Id for your user



来源:https://stackoverflow.com/questions/48201131/how-to-access-dialogflow-v2-api-from-a-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!