【问题标题】:Voice call using twilo in web application在 Web 应用程序中使用 twilio 进行语音通话
【发布时间】:2016-11-22 12:48:48
【问题描述】:

我们正在尝试为我们的网络应用程序使用语音呼叫。 我们尝试使用以下代码:

public class MakeCall {
  public static final String ACCOUNT_SID = "ACbXXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String AUTH_TOKEN = "545XXXXXXXXXXXXXXXXXXXXXXXX";
  public static final String TWILIO_NUMBER = "+185XXXXXXXXX";
  public static void main(String[] args) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
    Account mainAccount = client.getAccount();
    CallFactory callFactory = mainAccount.getCallFactory();
    Map<String, String> callParams = new HashMap<String, String>();
    callParams.put("From",TWILIO_NUMBER);
    callParams.put("To", "+919014512394");
    callParams.put("Url", "http://ahoy.twilio.com/voice/api/demo");
    Call call = callFactory.create(callParams);
    System.out.println(call.getSid());
  }
}

从上面的代码中,我们可以听到 twilio 客户的声音,即,欢迎使用 Twilio 语音演示应用程序。按 1 收听明天的天气预报。按 2 聆听歌曲。按 3 创建或加入会议桥。按 4 录制您的声音 5 秒钟。

其实我们是想通过twilio号码拨打其他手机号码

基本上我们是 twilio API 的新手。请指导我们 提前谢谢

【问题讨论】:

    标签: java web-applications twilio twilio-api twilio-click-to-call


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    您编写的代码将在您调用该端点时发起一个新调用。如果你想发起一个新呼叫,然后连接到另一个号码以便你们两个可以通话,你需要稍微改变一下,为你的号码做 Dials。

    这是一个完整的工作示例,向您展示如何选择要与之交谈的人,然后拨打该人的电话。

    package com.twilio;
    
    import com.twilio.twiml.Gather;
    import com.twilio.twiml.Method;
    import com.twilio.twiml.Play;
    import com.twilio.twiml.Say;
    import com.twilio.twiml.TwiMLException;
    import com.twilio.twiml.VoiceResponse;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.HashMap;
    
    public class TwilioServlet extends HttpServlet {
    
        public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
            // Create a dict of people we know.
            HashMap<String, String> callers = new HashMap<String, String>();
            callers.put("+14158675309", "Curious George");
            callers.put("+14158675310", "Boots");
            callers.put("+14158675311", "Virgil");
    
            String fromNumber = request.getParameter("From");
            String knownCaller = callers.get(fromNumber);
            String message;
            if (knownCaller == null) {
                // Use a generic message
                message = "Hello Monkey";
            } else {
                // Use the caller's name
                message = "Hello " + knownCaller;
            }
    
            // Create a TwiML response and add our friendly message.
            VoiceResponse twiml = new VoiceResponse.Builder()
                    .say(new Say.Builder(message).build())
                    // Play an MP3 for incoming callers.
                    .play(new Play.Builder("http://demo.twilio.com/hellomonkey/monkey.mp3").build())
                    .gather(new Gather.Builder()
                            .action("/handle-key")
                            .method(Method.POST)
                            .numDigits(1)
                            .say(new Say
                                    .Builder("To speak to a real monkey, press 1. Press any other key to start over.")
                                    .build())
                            .build()
                    )
                    .build();
    
            response.setContentType("application/xml");
            try {
                response.getWriter().print(twiml.toXml());
            } catch (TwiMLException e) {
                e.printStackTrace();
            }
        }
    }
    
    1. 您从包含所有电话号码的 HashMap 开始
    2. 按下数字后,将调用 /handle-key 端点。这就是拨打另一个号码的逻辑发生的地方

      package com.twilio;
      
      import com.twilio.twiml.Dial;
      import com.twilio.twiml.Number;
      import com.twilio.twiml.Say;
      import com.twilio.twiml.TwiMLException;
      import com.twilio.twiml.VoiceResponse;
      
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import java.io.IOException;
      
      public class TwilioHandleKeyServlet extends HttpServlet {
      
          @Override
          public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
      
              String digits = request.getParameter("Digits");
              VoiceResponse twiml;
              // Check if the user pressed "1" on their phone
              if (digits != null && digits.equals("1")) {
                  // Connect 310 555 1212 to the incoming caller.
                  Number number = new Number.Builder("+13105551212").build();
                  Dial dial = new Dial.Builder().number(number).build();
      
                  // If the above dial failed, say an error message.
                  Say say = new Say.Builder("The call failed, or the remote party hung up. Goodbye.").build();
      
                  twiml = new VoiceResponse.Builder().dial(dial).say(say).build();
              } else {
                  // If they didn't press 1, redirect them to the TwilioServlet
                  response.sendRedirect("/twiml");
                  return;
              }
      
              response.setContentType("application/xml");
              try {
                  response.getWriter().print(twiml.toXml());
              } catch (TwiMLException e) {
                  e.printStackTrace();
              }
          }
      }
      

    您可以阅读此内容的完整说明并在此quickstart 中找到其他示例。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 2014-02-20
      • 2017-11-14
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多