1

是否可以执行以下操作?我试过了,失败了!

我使用 Twilio 从一些 Java 代码发起调用,并且我正在使用 TwiML。然后我想启动另一个操作,例如发送 SMS、拨打电话或重定向到另一个 servlet。我无法让这些工作中的任何一个工作,并且想知道是否是因为 Twilio 正在发起呼叫,而不是用户拨打我的 Twilo 号码?希望我只是愚蠢!这是我在 url 文件中尝试过的(实际数字替换为 xxxx

尝试发送短信*

<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/voicemail_record.xml -->
<Response>
    <Say>
        Hi John, Are you alright?
        If you would like me to get a carer, please wait.
        Or if you are alright, just hang up.  
        <Pause length="6"/>

    </Say>
    <Sms from="+1747999xxxx" to="+6422671xxxx">The king stay the king.</Sms>

    <Say>
        Okay, I have asked your carer to contact you

    </Say>
</Response>

* *或尝试拨打号码

<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/voicemail_record.xml -->
<Response>
    <Dial>
        <Number>64 7 856 xxxx</Number>
    </Dial>
<Say>Goodbye</Say>
  </Response> 

或尝试重定向

<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/voicemail_record.xml -->
<Response>
    <Gather method="POST" numDigits="5" action="https://myappengineapp.appspot.com/secure/twilio">
        <Say>Please confirm your SMS subscriptiong by pressing 1.</Say>
    </Gather>
</Response>

这是 Java 代码 - 我没有使用 Twilio 辅助 API 库,因为它在 Google App Engine 上不起作用* ** * *

package testing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import nz.co.assistedfreedom.cron.GAEJCronServlet;

import org.apache.commons.codec.binary.Base64;

public class TestTwilioCall {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String, String> vars = new HashMap<String, String>();
        x
        vars.put("To", "+647856xxxx");
        vars.put("From", "+1747999xxxx");

        vars.put("Url", "http://www.xxx.com/assistedliving/redirect1");
        "https://myappengineapp.appspot.com/secure/twilio");

        String accountSid = "*********secret*********";
        String authToken = "*********secret*********";
        makeCall(vars, accountSid, authToken);

    }

public static void makeCall(Map<String,String> vars, String accountSid, String authToken){

        try {


            String encoded = "";
            if(vars!=null){
                for(String key: vars.keySet()){
                    try {
                        encoded += "&"+key+"="+ URLEncoder.encode(vars.get(key),"UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                encoded =encoded.substring(1);
            }           

            URL url = new URL("https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Calls");       

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

             String userpass = accountSid+":"+authToken;
                String encodeuserpass = new String(Base64.encodeBase64(userpass.getBytes()));
                                httpURLConnection.setRequestProperty ("Authorization", "Basic " + encodeuserpass);

            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");         

            OutputStreamWriter out = new OutputStreamWriter(httpURLConnection.getOutputStream());
            out.write(encoded);
            out.close();

            BufferedReader in = null;
            try {
                if(httpURLConnection.getInputStream()!=null){
                    in = new BufferedReader(
                            new InputStreamReader(
                                    httpURLConnection.getInputStream()));
                } 
            } catch(IOException e){
                e.printStackTrace();
                if(httpURLConnection.getErrorStream()!=null){
                    in = new BufferedReader(
                            new InputStreamReader(
                                    httpURLConnection.getErrorStream()));
                }
            }

            if(in==null) {
                System.out.println("Unable to read response from server");
            } 

            StringBuffer decodedString = new StringBuffer();
            String line;
            while ((line = in.readLine()) != null) {
                decodedString.append(line);
            }
            in.close();

            // get result code
            int responseCode = httpURLConnection.getResponseCode();
            System.out.println("response code is " + responseCode);

            } catch (Exception e) {
                  final Logger log2 = Logger.getLogger(GAEJCronServlet.class.getName());
                //  log2.log(Level.SEVERE, e.getMessage());
                e.printStackTrace();
            }
    }

}
4

1 回答 1

0

在使用 TwiML 响应一个调用时,您可以通过 REST API 发起一个新调用。

看看这个:REST API:拨打电话

于 2013-06-22T09:20:29.357 回答