转发Twilio电话仅在工作时间

时间:2014-10-05 09:10:52

标签: twilio

我花了好几个小时试图找到它,但它仍然让我感到困惑。

我是否只能在某些时间将电话转接到我的手机,还有语音信箱?从表面上看,这应该是如此简单 - 使用Twiml,或许?但是,我似乎并没有#34;得到它"。

谢谢, 南西

2 个答案:

答案 0 :(得分:5)

Twilio开发者传播者在这里。

您确实可以使用Twilio执行此操作,但您需要编写一些代码并将其部署在某个地方的Internet上。我们来看看吧。

我们假设您正在使用此TwiML来转发您的电话:

<Response>
  <Dial>+5551234567</Dial>
</Response>

并且您希望在非工作时间内使用Say TwiML verb&#39;时间:

<Response>
  <Say>The office is currently closed.</Say>
</Response>

您需要的是一些可以在它们之间进行选择的应用程序。例如,一个简单的Ruby和Sinatra应用程序将如下所示:

require 'sinatra'

post '/voice' do
  content_type 'text/xml'

  if Time.now.hour > 8 && Time.now.hour < 18
    "<Response>
      <Dial>+5551234567</Dial>
    </Response>"
  else
    "<Response>
      <Say>The office is currently closed.</Say>
    </Response>"
  end
end

请注意,我们只关注时间,而不是一周中的某一天。因此,您将在上午8点到下午6点之间收到电话。您可能希望根据您的需要使其更加复杂。

然后,您只需要将此应用程序的URL提供给Twilio。根据您可用的工具,您可以在自己的服务器上运行,也可以在某些云服务提供商(如Heroku,EngineYard,AppFog等)上运行。大多数都有关于如何部署应用程序的非常好的文档。

希望这有帮助!

答案 1 :(得分:1)

使用Twilio功能,现在更容易了。您无需在任何地方托管任何东西。只需创建一个链接到传入语音呼叫事件的新功能,然后设置您的配置号码,以便在新呼叫到达时呼叫该功能。这是一个可以修改以适合您的偏好的工作示例;它包括拨号扩展名,但如果需要,您可以将其删除。

请注意,它没有DST逻辑,如果您需要,那么您将需要使用moment()库进行一些额外的工作。

https://gist.github.com/ChristopherThorpe/521bfdbd903ac7628ac01f8bb1d651a5

  exports.handler = function(context, event, callback) {
  const moment = require('moment');

  //// Useful for debugging
  //const util = require('util');
  //console.log(util.inspect(context.getTwilioClient()));
  //console.log(util.inspect(event));

  // be sure to update numDigits below to match, or delete it for variable length
  let phoneBook = {
    "888" : "+1-800-800-8000", // super 8 motel
    "666" : "+1-800-466-8356", // motel 6
    "000" : null
  };
  let callerId = event.Caller; // || "+1-000-000-0000"; // default caller ID
  let digits   = event.Digits;

  let twiml = new Twilio.twiml.VoiceResponse();

  if (digits && phoneBook[digits]) {
    twiml.say("Dialing extension " + digits);
    twiml.dial({ callerId: callerId }, phoneBook[digits]);
    twiml.hangup();
  }

  // Twilio time is in UTC. This allows 10 am to 7 pm PDT, or 9 am to 6 pm PST, weekdays.
  // Twilio doesn't seem to have https://momentjs.com/timezone/ installed.
  if ((moment().hour() >= 17 || moment().hour() < 2) && moment().isoWeekday() <= 5) {
    let gather = twiml.gather({ numDigits: 3, timeout: 3 });
    gather.say("Thank you for calling COMPANY NAME. Please dial your party's extension, or, hold, to leave a message.");
  } else {
    twiml.say("Thank you for calling COMPANY NAME. You have reached us outside business hours.");
  }
  twiml.redirect("http://twimlets.com/voicemail?Email=[YOUR-EMAIL]&Message=Please%20leave%20a%20message.&Transcribe=true");
  callback(null, twiml);
};
相关问题