Dialogflow Webhook Not Working When Called from Python Code

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật