简体   繁体   中英

How to integrate Dialogflow with Django python and consume RESTful APIs?

Good day everyone, I am currently developing a chatbot using Google's Dialogflow and I am really in need of help.

I have already seen How to integrate Dialogflow with Django (Python)? but I am still confuse and I have no idea what to do next. Our Django project has already been set up and I have already created my agent in Dialogflow, I also learned how to use ngrok so to connect Dialogflow to fulfillment/webhook. I am new to Django python as well and these are just the things that I've done so far.

I've already searched almost, if not all, tutorials/articles/videos on how to integrate Dialogflow to Django python with RESTful APIs and I found none on how to do it. Most tutorial uses Flask and NodeJS as their backend.

Although this tutorial uses Flask https://www.pusher.com/tutorials/chatbot-flask-dialogflow/ , I tried doing it just to have an idea on how to integrate dialogflow to a backend and call RESTful APIs. I manage to replicate what has been done in the tutorial, but unfortunately, I didn't understood the coding part in the backend or the code in setting up the webhook as I am very new to it.

I am really lost right now and I really have no idea what to code in the backend(Django) in order to connect the RESTful APIs to Dialogflow. I would be very much thankful if you guys could help me and give some detailed explanation of codes or step by step in how to integrate Dialogflow to Django and consume RestFUL APIs.

While you can use the Dialogflow Python API for text requests, I personally only use their POST interface. As it is more straightforward for me to use. The good thing is, you do not need to mess around with the webhook.

All you need is the Client Access Token from your Dialogflow Console. Where it says: YOUR_CLIENT_ACCESS_TOKEN' you just enter you access Token.

Then you can send the HTTP POST request like this:

curl \
https://api.dialogflow.com/v1/query?v=20150910 \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_CLIENT_ACCESS_TOKEN' \
-d '{
  "lang": "en",
  "query": "I need apples",
  "sessionId": "12345",
  "timezone": "America/New_York"
}'

https://dialogflow.com/docs/reference/agent/query

I usually use the "Requests" Package from Python, which you can install with

pip install requests

Requests example

I think it´s pretty and easy to use. Only when you want to use more than just sending queries to Dialogflow, for example, adding or deleting Intents, you will need a bit more than this. But for your purpose this should suffice. I hope it helped, feel free to ask some more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM