[Python] Conference not listed when created via Twiml Callback

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I configured a phone number, so that an incoming call triggers an HTTP callback. On my server I am running a Python script which listens for that callback and answeres with a "dial conference command".

    That works fine. When I call my phone number I join the conference and I can hear the conference wait music. Another call also works and the two calls come together in the conference and can talk to each other. But when using client.conferences.list() I cannot see this conference.


    1. Python code listening for the callback:

      import os
      import sys
      import time
      import json
      import uvicorn

      from dotenv import load_dotenv
      from fastapi import FastAPI, WebSocket, Form, Request, Response, Query
      from fastapi.responses import HTMLResponse, JSONResponse
      from fastapi.middleware.cors import CORSMiddleware
      from loguru import logger
      from twilio.twiml.voice_response import VoiceResponse
      from twilio.rest import Client

      ###############################################################################
      # init
      ###############################################################################
      load_dotenv(f".env")

      app = FastAPI()

      app.add_middleware(
      CORSMiddleware,
      allow_origins=["*"], # Allow all origins for testing
      allow_credentials=True,
      allow_methods=["*"],
      allow_headers=["*"],
      )

      logger.remove()
      logger.add(sys.stderr, level="DEBUG")

      ###############################################################################
      # Routes and WebSocket
      ###############################################################################
      @app.post("/twilio-call")
      async def start_call(request: Request, AccountSid: str = Form(...), CallSid: str = Form(...), From: str = Form(...), To: str = Form(...)):
      # push into conference
      conference_room_name = "test-1234"
      logger.info(f"|{CallSid}| Caller joins conference room {conference_room_name}")
      response = VoiceResponse()
      dial = response.dial()
      dial.conference(conference_room_name)

      logger.info(f"|{CallSid}| xml=" + str(response))
      return Response(str(response), media_type='text/xml')

      ###############################################################################
      # main
      ###############################################################################
      if __name__ == "__main__":
      app_port = int(os.getenv("APP_PORT"))

      uvicorn.run(app, host="0.0.0.0", port=app_port)


      When calling, I can see in my console:

      INFO: Started server process [1023305]
      INFO: Waiting for application startup.
      INFO: Application startup complete.
      INFO: Uvicorn running on http://0.0.0.0:3001 (Press CTRL+C to quit)
      2025-09-09 16:23:44.896 | INFO | __main__:start_call:41 - |CA572e178803ea4e8d6b3f242a2ac03b58| Caller joins conference room test-1234
      2025-09-09 16:23:44.897 | INFO | __main__:start_call:46 - |CA572e178803ea4e8d6b3f242a2ac03b58| xml=<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Conference>test-1234</Conference></Dial></Response>
      INFO: 108.130.4.158:0 - "POST /twilio-call HTTP/1.1" 200 OK


      and I hear the conference wait music.


    2. List conferences

      While staying in the conference I use the following code to list all conferences:

      from twilio.rest import Client
      from twilio.twiml.voice_response import VoiceResponse
      from dotenv import load_dotenv
      import os

      # init
      env = os.getenv("ENVIRONMENT", "dev")
      load_dotenv(f".env.{env}")

      account_sid = os.environ.get("TWILIO_ACCOUNT_SID", "")
      auth_token = os.environ.get("TWILIO_AUTH_TOKEN", "")
      conference_room_name = "test-1234"

      # create client
      client = Client(account_sid, auth_token)

      # list conferences
      print("Conference-List:")
      conferences = client.conferences.list()
      for conf in conferences:
      print(f"SID: {conf.sid} | FriendlyName: {conf.friendly_name} | DateCreated: {conf.date_created} | Status: {conf.status}")


      Result: no conferences found.


    3. But when using this code to initiate an outbound call, the conference is listed

      from twilio.rest import Client
      from twilio.twiml.voice_response import VoiceResponse
      from dotenv import load_dotenv
      import os

      # Init
      load_dotenv(f".env")

      account_sid = os.environ.get("TWILIO_ACCOUNT_SID", "")
      auth_token = os.environ.get("TWILIO_AUTH_TOKEN", "")
      conference_room_name = "test-1234"

      # Twilio Client
      client = Client(account_sid, auth_token)

      # Outbound Call with TwiML to join conference
      call = client.calls.create(
      to="+49...",
      from_="+49...",
      twiml=f'''
      <Response>
      <Dial>
      <Conference>{conference_room_name}</Conference>
      </Dial>
      </Response>
      '''
      )

      input("Press Enter to list conferences...")

      print("Conference-List:")
      conferences = client.conferences.list()
      for conf in conferences:
      print(f"SID: {conf.sid} | FriendlyName: {conf.friendly_name} | DateCreated: {conf.date_created} | Status: {conf.status}")


      Output:

      Press Enter to list conferences...
      Conference-List:
      SID: CF82fd459cce185021d6e18e18141c8c0e | FriendlyName: test-1234 | DateCreated: 2025-09-09 16:40:23+00:00 | Status: in-progress

    Continue reading...

Compartilhe esta Página