【问题标题】:Twilio outgoing call idTwilio 拨出呼叫 ID
【发布时间】:2016-05-20 06:22:14
【问题描述】:

我需要一些帮助。

我一直在阅读有关此 API Twilio 的信息,因为必须解决一些我没有做的代码。

它正在用 PHP 编写一个页面,以便给客户打电话。有一个进程经常运行并检查日志或 cvs 并在数据库中插入一条带有 info Twilio 的记录。

理想情况下,如果在呼出结束时,可以知道呼叫的唯一 ID,并将其与一些 cmets 一起保存。

如何获取客户端调用的参数?还是有其他方法? (持续时间、Sid、到等)

我已经看过了,但我不明白

谢谢!!

https://www.twilio.com/docs/api/rest/making-calls

https://www.twilio.com/docs/api/rest/call

<?php 
require_once('Services/Twilio.php'); // Loads the library
$API_VERSION = '2010-04-01';
$accountSid = 'AC7d3444f83611xxxxxxxxxxxxxxxx';
$authToken  = 'd72d5f036e96abexxxxxxxxxxxxxx';

$capability = new Services_Twilio_Capability($accountSid, $authToken);
$capability->allowClientOutgoing('AP29cb5d9e0dcxxxxxxxxxxxxxxxxxx');
$capability->allowClientIncoming('name');
$token = $capability->generateToken();
//error_log("token is $token");
?>

<script type="text/javascript">     
      Twilio.Device.setup("<?php echo $token; ?>");     
      function call() {         
      params = {"PhoneNumber": $("#number_no").val(), "callerid": $("#callerid").val(), "agent_id": $("#agent_id").val(), "lead_id": $("#lead_id").val()};
      Twilio.Device.connect(params);
      }     
      function hangup() {           
        Twilio.Device.disconnectAll();
        //Twilio.Device.disconnect();
      }       
  </script>

更新内容时调用此代码

<script>
$(document).ready(function() {

    $('.call_status').change(function()
    {
        var status = $(this).val();
        var rec_val = $(this).parent().find('.rec_val').val();
        var pledge_amt = $('#pledge_amt'+rec_val).val();
        hangup();
    $.ajax({
    url: '<?php echo site_url('agent/agent_status/'); ?>',
    type: 'post',
    data: {'rec_val': rec_val, 'status': status, 'pledge_amt': pledge_amt},
    success:function(resp)
    {
        if(resp)
        {
            window.location.href="<?php echo site_url('agent/call_leads/'); ?>";
            //$('.msg').html("Status Updated Successfully!").fadeOut(5000);

        } else 
            {
             alert('Please check your selected data');
            }
    },
    error:function(resp)
    {
        alert(resp);
    }
    });

    });
    jQuery('.status_update').fadeOut(5000);
});
</script>

【问题讨论】:

标签: javascript php api twilio


【解决方案1】:

这里是 Twilio 开发者宣传员。

当你create the call, you get a connection objectconnection object contains all the parameters 您已在 REST API 文档中阅读到有关拨打电话的信息。您可以通过这种方式获取呼叫 SID(它的唯一 ID)。在下面的代码中,我使用了调用时创建的connection对象来注销调用的参数:

<script type="text/javascript">     
      Twilio.Device.setup("<?php echo $token; ?>");     
      function call() {         
          params = {"PhoneNumber": $("#number_no").val(), "callerid": $("#callerid").val(), "agent_id": $("#agent_id").val(), "lead_id": $("#lead_id").val()};
          var connection = Twilio.Device.connect(params);
          console.log(connection.parameters);
      }     
  </script>

如果这有帮助,请告诉我!

编辑:

要在挂断函数中使用此连接,您需要将连接对象保存在调用函数范围之外。您可以通过将currentConnection 定义为两个函数之外的变量,然后在操作发生时设置和使用它来做到这一点。请参阅下面的更新代码(查看 cmets 了解更多信息):

<script type="text/javascript">     
      Twilio.Device.setup("<?php echo $token; ?>");
      // define currentConnection here, but don't set it to anything.
      var currentConnection = null;

      function call() {         
          params = {"PhoneNumber": $("#number_no").val(), "callerid": $("#callerid").val(), "agent_id": $("#agent_id").val(), "lead_id": $("#lead_id").val()};
          // create and set the currentConnection
          currentConnection = Twilio.Device.connect(params);
          console.log(currentConnection.parameters);
      }

      function hangup() {
        console.log("Do stuff with parameters");
        console.log(currentConnection.parameters)
        Twilio.Device.disconnectAll();

        // reset currentConnection until the next call is made
        currentConnection = null;
      } 
</script>

【讨论】:

  • 谢谢@philnash,但是我在 js 中是个菜鸟,我怎么能在函数 hangup() 中取这个值
  • 我已经更详细地编辑了答案。希望这会有所帮助。
  • 我认为这不起作用.. TypeError: currentConnection is null in function hangup
  • 抱歉,或者更好的是,我可以将一两个参数传递给 window.open 吗?
  • 万岁!很高兴你能成功!如果有帮助,请务必将答案标记为正确。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多