菜鸟教程小白 发表于 2022-12-12 13:08:55

python - Twilio 可编程语音调用立即完成


                                            <p><p>我正在使用带有 Swift 和 Python 的新 Twilio 可编程语音 SDK。我已经开始了各自的快速入门项目,并且大部分情况下都有效。我能够获得有效的访问 token ,能够成功创建调用,甚至能够接听调用。问题出在房子的调用方。</p>

<p>当我尝试通过 Swift SDK 调用电话时,通话在另一端开始响铃之前就已断开。</p>

<p>我在 Twilio 文档中读到,如果您不处理 <code>status_callback</code> 事件,<code>client.calls.create</code> 函数将立即返回完成状态。我试图添加它,但每当我这样做时,我都会收到一条错误消息,指出键 <code>status_callback</code> 不是 <code>client.calls.create</code> 函数的有效参数。此外,我在任何地方都找不到任何实际如何处理通话状态的示例。</p>

<p>我的问题是我在这里做错了什么?任何帮助将不胜感激。</p>

<p>这是我的 Python 代码</p>

<pre><code>@app.route(&#39;/outgoing&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])
def outgoing():

account_sid = os.environ.get(&#34;ACCOUNT_SID&#34;, ACCOUNT_SID)
api_key = os.environ.get(&#34;API_KEY&#34;, API_KEY)
api_key_secret = os.environ.get(&#34;API_KEY_SECRET&#34;, API_KEY_SECRET)

from_number =
to_number =

client = Client(api_key, api_key_secret, account_sid)
call = client.calls.create(url=&#39;http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient&#39;, to=to_number, from_=from_number)

# return str(call.sid)
resp = twilio.twiml.Response()
resp.say(&#34;Thank you for calling&#34;);
return str(resp)
</code></pre>

<p>这是我的相关 iOS 代码。请记住,这不是我的全部来源。我只提供了在这种情况下应该有的东西。我的完整来源确实包括处理注册表和邀请代表。我也没有包括显示/隐藏我的事件调用 UI 的源,因为这没有问题。这只是为了说明我是如何调用电话和接听电话完成委托(delegate)的。</p>

<pre><code>class VoiceManager: NSObject, PKPushRegistryDelegate, TVONotificationDelegate, TVOCallDelegate, AVAudioPlayerDelegate {

    //MARK: - Singleton

    static let sharedManager = VoiceManager()

    //MARK: - Private Constants

    private let baseURLString =
    private let accessTokenEndpoint = &#34;/accessToken&#34;

    //MARK: - Private Variables

    private var deviceTokenString:String?

    private var callInvite: TVOCallInvite?
    private var call: TVOCall?
    private var status: VoiceStatus = .idle

    //MARK: - Getters

    private func fetchAccessToken() -&gt; String? {

      guard let accessTokenURL = URL(string: baseURLString + accessTokenEndpoint) else {
            return nil
      }

      return try? String.init(contentsOf: accessTokenURL, encoding: .utf8)
    }

    func placeCall(withParameters params: VoiceParameters, completion: @escaping (_ success: Bool, _ error: VAError?) -&gt; Void) {

      if (call != nil) {
            call?.disconnect()
            completion(false, .phoneCallInProgress)
            status = .callEnded
            hideActiveCallUI()

      } else {

            guard let accessToken = fetchAccessToken() else {
                completion(false, .phoneAccessTokenFetchFailed)
                return
            }

            guard let paramsDict = params.toDictionary() else {
                completion(false, .phoneAccessTokenFetchFailed)
                return
            }

            playOutgoingRingtone(completion: { in

                if let strongSelf = self {

                  strongSelf.call = VoiceClient.sharedInstance().call(accessToken, params: [:], delegate: strongSelf) //NOTE: The params here are not necessary as the phone numbers for now are hard coded on the server

                  if (strongSelf.call == nil) {
                        strongSelf.status = .callEnded
                        completion(false, .phoneCallFailed)
                        return

                  } else {
                        strongSelf.status = .callConnecting
                        self?.showActiveCallUI(withParameters: params)
                        completion(true, nil)
                  }
                }
            })
      }
    }

    // MARK: TVOCallDelegate
    func callDidConnect(_ call: TVOCall) {

      NSLog(&#34;callDidConnect:&#34;)

      self.call = call
      status = .inCall

      routeAudioToSpeaker()
    }

    func callDidDisconnect(_ call: TVOCall) {

      NSLog(&#34;callDidDisconnect:&#34;)

      playDisconnectSound()

      self.call = nil
      status = .callEnded

      hideActiveCallUI()
    }

    func call(_ call: TVOCall, didFailWithError error: Error) {

      NSLog(&#34;call:didFailWithError: \(error)&#34;);

      self.call = nil
      status = .callEnded
      hideActiveCallUI()
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这里是 Twilio 开发者宣传员。</p>

<p>您的 Swift 代码表明您的电话号码现在在服务器上是硬编码的。正如 Robert 所说,问题是当您从 Twilio 获得回调到您的 <code>/outbound</code> 端点时,您正在使用 REST API 生成调用。</p>

<p>实际发生的情况是,当您 <a href="https://www.twilio.com/docs/quickstart/client/ios" rel="noreferrer noopener nofollow">generate the call in Swift</a>在启动应用程序调用的设备上。然后,Twilio 向您的 <code>/outbound</code> 端点发出 HTTP 请求,以查看如何处理该调用。所以,而不是 <a href="https://www.twilio.com/docs/api/rest/making-calls" rel="noreferrer noopener nofollow">generating a new call with the REST API</a> ,您需要回复 <a href="https://www.twilio.com/docs/api/twiml" rel="noreferrer noopener nofollow">TwiML</a>告诉 Twilio 下一步如何处理调用。</p>

<p>在这种情况下,听起来您正在尝试 <a href="https://www.twilio.com/docs/api/twiml/dial" rel="noreferrer noopener nofollow">dial</a>直接上另一个 <a href="https://www.twilio.com/docs/api/twiml/number" rel="noreferrer noopener nofollow">number</a> .为此,您应该尝试以下响应:</p>

<pre><code>@app.route(&#39;/outgoing&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])
def outgoing():
from_number =
to_number =

resp = twilio.twiml.Response()
with resp.dial(callerId=from_number) as r:
    r.number(to_number)
return str(resp)
</code></pre>

<p>如果有帮助,请告诉我。</p></p>
                                   
                                                <p style="font-size: 20px;">关于python - Twilio 可编程语音调用立即完成,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42673441/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42673441/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: python - Twilio 可编程语音调用立即完成