How to integrate Dialogflow with Django (Python)?

2019-05-31 00:56发布

问题:

I'm developing an assistant (bot) with Dialogflow, and i have this Django project where I have to extract data and then expose it through the bot, this is going to be stored in a local platform.

I worked with Dialogflow and it's integration before but with Node.js and Javascript, with Django (python) is a brand new challenge and I'm confused.

Until now I have the following:

  • I know I can do it with this package: https://github.com/dialogflow/dialogflow-python-client-v2
  • I add a url for a webhook, right now this works locally only, like this:

    from django.http import HttpResponse
    from django.views.decorators.csrf import csrf_exempt
    import dialogflow
    
    
    @csrf_exempt
    def webhook(request):
        return HttpResponse('Works like a charm!')
    

    and in urls.py I have:

    url(r'^webhook$', views.webhook, name='webhook')
    

And that's all I don't know how to proceed after this, I'm blocked and I don't know how yto make the integration and what's missing, any recommendations?

回答1:

Steps

  • Make one html file for your chatbot UI by rendering it with one url-endpoint and view for displaying purpose of your html file say bot.html.
  • For dialogflow I first suggest you to build a dialogflow agent as per you requirement.
  • for webhook of agent provided by your djangoserver which is routed with localtunnel's public IP (for developement purpose use localtunnel like ngrok to your django's server)
  • then add this link to fulfillment of agent.

Hey you can refer this LINK using that you just need to

  • Create one url-endpoint and view say /chatbot which will accepts your text send by bot by AJAX request. This text is then passes to parameter texts in that above functions in link. But make some changes in that functions like without printing fulfillment text just return it.
  • Then in above url-endpoint /chatbot return the response as fulfillmentText return by that function in link.
  • If you wanna use some data of your databases then you can create django-models as tables in normal database, for that check the models - docs. then you can access data by using models.objects.all() etc.