we are integrating the Dialogflow webhook service using FastAPI. Everything is working fine, but when we call the Dialogflow API from our Python code, the webhook response is not received. However, the webhook is working on the Dialogflow console’s right-side message tab.
Dialogflow Webhook Code
import requests, json
from fastapi import APIRouter
from fastapi_utils.cbv import cbv
from fastapi_sqlalchemy import db
from termcolor import colored
from source.controllers.dialogflows import DialogFlowController
from source.controllers.locations import LocationController
from source.controllers.users import UserController
from pywa.types import SectionList, Section, SectionRow
from db.models.commons.locations import City
from source.controllers.whatsapps import WhatsAppController
from db.models.transportations.masters import VehicleMaster
router = APIRouter()
@cbv(router)
class DialogFlowAPI(WhatsAppController, DialogFlowController, LocationController, UserController):
@router.post("/v1/dialogflow")
async def webhook_receiver(self, data: dict):
params = data["queryResult"]["outputContexts"][0]["parameters"]
print("CURRENT_DIALOG_FLOW_RESPONSE: {response}".format(response=data))
print("CURRENT_INTENT: {intent}".format(intent=data["queryResult"]["intent"]["displayName"]))
print("CURRENT_PARAMETERS: {parameters}".format(parameters=data["queryResult"]["outputContexts"][0]["parameters"]))
match data["queryResult"]["action"]:
case "welcomeIntent":
return self.reply_message_with_follow_up(
event_name="mobileIntent", language_code="en-US"
)
case "mobileIntent":
if len(str(int(params["mobileNumber"]))) != 10:
return self.reply_message_with_fullfillment_message(
message="Please enter a valid 10 digit mobile number"
)
return self.reply_message_with_follow_up(
event_name="locationIntent", language_code="en-US"
)
case "locationIntent":
return self.reply_message_with_follow_up(
event_name="serviceIntent", language_code="en-US"
)
Dialogflow API Code
async def send_to_dialogflow(self, message: str, session_id: str, language_code):
session_client = dialogflow.SessionsClient(credentials=service_account.Credentials.from_service_account_info(
json.loads(base64.b64decode(
os.environ['GOOGLE_APPLICATION_CREDENTIAL']
).decode('utf-8'))
))
session = session_client.session_path('incilo-bot', session_id)
text_input = dialogflow.TextInput(text=message, language_code=language_code)
query_input = dialogflow.QueryInput(text=text_input)
response = session_client.detect_intent(
request={'session': session, 'query_input': query_input}
)
if len(response.query_result.fulfillment_messages) == 2:
return {
'fulfillment_text': response.query_result.fulfillment_text,
'intent_name': response.query_result.intent.display_name,
'intent_last_response': True
}
return {
'fulfillment_text': response.query_result.fulfillment_text,
'intent_name': response.query_result.intent.display_name,
'intent_last_response': False
}
Dialogflow API Response when no webhook fulfillment enabled.
{
"responseId":"077822dc-95b8-41f2-8306-33ae03cf49d5-14a02368",
"queryResult":{
"queryText":"english",
"action":"welcomeIntent",
"parameters":{
"language":"English"
},
"allRequiredParamsPresent":true,
"outputContexts":[
{
"name":"projects/incilo-bot/agent/sessions/wa.917828497905/contexts/contextdata",
"lifespanCount":5,
"parameters":{
"language":"English",
"language.original":"english"
}
},
{
"name":"projects/incilo-bot/agent/sessions/wa.917828497905/contexts/__system_counters__",
"parameters":{
"no-input":0.0,
"no-match":0.0,
"language":"English",
"language.original":"english"
}
}
],
"intent":{
"name":"projects/incilo-bot/agent/intents/b36a2014-6b4d-43b6-92fb-437c288e76f0",
"displayName":"welcomeIntent"
},
"intentDetectionConfidence":1.0,
"languageCode":"en"
},
"originalDetectIntentRequest":{
"payload":{
}
},
"session":"projects/incilo-bot/agent/sessions/wa.917828497905"
}
Dialogflow API Response when webhook fulfillment enabled
{
"fulfillment_text":"",
"intent_name":"",
"intent_last_response":false
}
Dialogflow Console Response – Webhook Enabled
{
"responseId":"5426d7be-fef2-4dea-8c09-19940ab3917c-14a02368",
"queryResult":{
"queryText":"english",
"action":"mobileIntent",
"parameters":{
"mobileNumber":""
},
"fulfillmentText":"Please enter your mobile number.",
"fulfillmentMessages":[
{
"text":{
"text":[
"Please enter your mobile number."
]
}
}
],
"outputContexts":[
{
"name":"projects/incilo-bot/agent/sessions/9f7e3622-a3e6-dbde-942b-ed81508483a2/contexts/dc9f573a-4416-4fe0-8622-54bc6669271b_id_dialog_context",
"lifespanCount":2,
"parameters":{
"mobileNumber.original":"",
"mobileNumber":""
}
},
{
"name":"projects/incilo-bot/agent/sessions/9f7e3622-a3e6-dbde-942b-ed81508483a2/contexts/mobileintent_dialog_context",
"lifespanCount":2,
"parameters":{
"mobileNumber.original":"",
"mobileNumber":""
}
},
{
"name":"projects/incilo-bot/agent/sessions/9f7e3622-a3e6-dbde-942b-ed81508483a2/contexts/mobileintent_dialog_params_mobilenumber",
"lifespanCount":1,
"parameters":{
"mobileNumber":"",
"mobileNumber.original":""
}
},
{
"name":"projects/incilo-bot/agent/sessions/9f7e3622-a3e6-dbde-942b-ed81508483a2/contexts/mobileintent",
"lifespanCount":1,
"parameters":{
"mobileNumber":"",
"mobileNumber.original":""
}
},
{
"name":"projects/incilo-bot/agent/sessions/9f7e3622-a3e6-dbde-942b-ed81508483a2/contexts/contextdata",
"lifespanCount":5,
"parameters":{
"mobileNumber.original":"",
"mobileNumber":"",
"language.original":"english",
"language":"English"
}
},
{
"name":"projects/incilo-bot/agent/sessions/9f7e3622-a3e6-dbde-942b-ed81508483a2/contexts/__system_counters__",
"lifespanCount":1,
"parameters":{
"no-match":0,
"no-input":0,
"mobileNumber":"",
"mobileNumber.original":""
}
}
],
"intent":{
"name":"projects/incilo-bot/agent/intents/dc9f573a-4416-4fe0-8622-54bc6669271b",
"displayName":"mobileIntent"
},
"intentDetectionConfidence":1,
"diagnosticInfo":{
"accumulated_webhook_latency_ms":148
},
"languageCode":"en"
}
}
The problem is unable to get the fulfillment response when we are calling the Dialogflow API. But its working on the Dialogflow Console.
1