是否可以执行以下操作?我尝试过并失败了!
我使用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();
}
}
}
最佳答案
在使用TwiML给出一个呼叫的响应的同时,您可以通过REST API发起一个新的呼叫。
查看以下内容:REST API: Making calls
关于java - 在Twilio发起的通话中发送短信或给其他人打电话,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15538339/