Artificial Intelligence

Dialogflow Tutorial: Back Functionality in Chatbot using Python + Django | by Pragnakalp Techlabs

Source
[
[
[
"Response Message of stage 1"
],
[
"Output Context of stage 1"
]
],
[
[
"Response Message of stage 2"
],
[
"Output Context of stage 2"
]
],
.
.
.
[
[
"Response Message of stage N"
],
[
"Output Context of stage N"
]
]
]

1. Conversational AI: Code/No Code

2. Chatbots 2.0: Simplifying Customer Service with RPA and AI

3. Question Answering on Medical conversation

4. Automating WhatsApp with NLP: Complete guide

pip install Django
django-admin startproject Back_Function
Back_Function/
Manage.py
Back_Function/
__init__.py
Settings.py
Urls.py
Wsgi.py
 cd Back_Function
python manage.py migrate
python manage.py startapp myapp

myapp/
__init__.py
Admin.py
Apps.py
Models.py
Tests.py
Views.py

INSTALLED_APPS = [
'Django.contrib.admin',
'Django.contrib.auth',
'Django.contrib.contenttypes',
'Django.contrib.sessions',
'Django.contrib.messages',
'Django.contrib.staticfiles',
'myapp',
]
from django.urls import path
from myapp import views
urlpatterns = [
# define a route for home
path('home/', views.home, name='home'),
path('webhook/', views.webhook, name='webhook')
]
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
import json
from df_response_lib import *

# define home function
def home(request):
return HttpResponse('Hello World!')

@csrf_exempt
def webhook(request):

ff_response = fulfillment_response()

# build a request object of the qebhook request
req = json.loads(request.body)

# get action from json
action = req.get('queryResult').get('action')

# Django responses from webhook

# response for the Stage_1 intent
if action == "S_1":
Text_response = "You are in Stage 1."

# response for the Stage_2 intent
elif action == "S_2":
Text_response = "You are in Stage 2."

# response for the Stage_3 intent
elif action == "S_3":
Text_response = "You are in Stage 3."

# response for the Stage_4 intent
elif action == "S_4":
Text_response = "You are in Stage 4."

storage = []
context = []

# loop over the all the context
mess = req.get('queryResult').get("outputContexts")
for m in mess:
name = m.get('name')
# save the parameter value of 'back' context which is our saved last responses
if name[-len("back"):] == "back":
stored = m.get("parameters").get("parameter")
for k in stored:
storage.append(k)
else:
# contexts of the current stage
context.append(m)

# when request for back is arise, get the last response from the storage which was saved in the parameter of the back context and remove the currernt response from the data.
if action == "back":
temp = storage[-2]
ff_context = temp[1]
ff_text = temp[0]
storage.pop(len(storage)-1)
else:
ff_text = ff_response.fulfillment_text(Text_response)
new_data = [ff_text, context]
storage.append(new_data)

# output context from the webhook for back functionality
contexts = [['back', 100, 'parameter': storage]]
# get session name from fulfilment reqest
session = req.get("session")

# set the output context in the webhook response
ff_out_context = ff_response.output_contexts(session, contexts)

# Also activate the contexts of the last response
if action == "back":
for i in range(len(ff_context)):
ff_out_context.get('output_contexts').append(ff_context[i])

# set webhook response for the requested action
reply = ff_response.main_response(fulfillment_text = ff_text, output_contexts = ff_out_context)

# webhook response
return JsonResponse(reply, safe = False)

python manage.py runserver
ngrok http 8000
ALLOWED_HOSTS = [ '47901fda.ngrok.io', '127.0.0.1' ]

For more updates check below links and stay updated with News AKMI.
Life and style || E Entertainment || Automotive News || Consumer Reviewer || Most Popular Video Games || Lifetime Fitness || Giant Bikes

Source

Tags
Show More

Related Articles

Back to top button

usa news wall today prime news newso time news post wall

Close