[英]Dumbfounded by Twilio API to send a simple text message
我有一个Twilio的帐户,我正在使用它以编程方式通过PHP脚本发送自己的短信。 现在我想在我正在编写的服务中使用此功能。 我的问题是缺乏关于如何在Java中实现功能的好例子。 似乎只有一个我想要做的例子,但我在eclipse(霓虹灯)中不断出现错误,说PhoneNumber(String)的构造函数中有错误。 我先向您展示我的代码然后再解释一下。
我有这两个类,一个用于存储我的用户凭据,另一个用于保存邮件收件人数据(名称和电话号码).... ps原谅格式。
/**
* Class contains my credentials with Twilio.com
* and are read from a file located at /sms_textdata/twiliocredentials.txt
*/
public class SMSCredentials
{
private final String userID;
private final String accessCode;
private final String senderNumber;
public SMSCredentials(String userID, String accessCode, String senderNumber)
{
this.userID = userID;
this.accessCode = accessCode;
this.senderNumber = senderNumber;
}
public String getUserID()
{
return userID;
}
public String getAccessCode()
{
return accessCode;
}
public String getSenderNumber()
{
return senderNumber;
}
}
/**
* A class that models a recipient of a text message
* Recipient list is located at /sms_textdata/smsrecipients.txt
*/
public class MessageRecipient
{
//first name
private String name;
//phone number
private String phoneNumber;
public MessageRecipient(String name, String phoneNumber)
{
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName()
{
return name;
}
public String getNumber()
{
return phoneNumber;
}
}
我实现了上面两个类的第三个类。 我打算从两个文本文件中读取凭证数据和收件人数据。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.rest.lookups.v1.PhoneNumber;
/**
* This class is responsible for sending SMS text messages
* to a list of recipients.
*/
public class SMSAlert
{
private SMSCredentials credentials;
private ArrayList<MessageRecipient> recipients;
public SMSAlert()
{
this.getCredentials();
this.createRecipientList();
}
/**
* send a SMS text message to a list of recipients
*/
public void sendSMSTextMessage()
{
//send messages to a list of recipients
Twilio.init(credentials.getUserID(), credentials.getAccessCode());
for(MessageRecipient rec : recipients)
{
Message message = Message.creator(new PhoneNumber(rec.getNumber()),
new PhoneNumber(credentials.getSenderNumber()),
" Hey " + rec.getName() + " the garage door is open!")
.create();
//System.out.println(message.getSid());
}
}
/**
* create an object containing the credentials required from Twilio.com
*/
private void getCredentials()
{
String credentialsFile;
Scanner credentialIn = null;
String id = null;
String code = null;
String number = null;
try
{
credentialsFile = "~/Documents/twiliocredentials.txt";
credentialIn = new Scanner(new File(credentialsFile));
// as there are only two strings in this file, the loop will execute
//only once, and assign the values to a new object of type SMSCredentials.
while(credentialIn.hasNext())
{
id = credentialIn.next();
code = credentialIn.next();
number = credentialIn.next();
}
//create the credentials object from a file
credentials = new SMSCredentials(id, code, number);
}
catch(IOException e)
{
e.getStackTrace();
}
finally
{
credentialIn.close();
}
}
/**
* create a list of recipients from a file to send text messages to.
*/
private void createRecipientList()
{
recipients = new ArrayList<MessageRecipient>();
String userFile;
Scanner recipientsIn = null;
String name;
String number;
try
{
userFile = "~/Documents/smsrecipients.txt";
recipientsIn = new Scanner(new File(userFile));
//file format is name : number pair on one line of text
//one object is instantiated per line/user
while(recipientsIn.hasNext())
{
name = recipientsIn.next();
number = recipientsIn.next();
//instantiate and add a user to the list
recipients.add(new MessageRecipient(name, number));
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
recipientsIn.close();
}
}
}
我在eclipse中遇到的错误来自sendSMSTextMessage()方法。 Eclipse给了我错误:
The constructor PhoneNumber(String) is undefined
这是一个Maven项目,我拥有所有依赖项.....我缺少什么? 有没有人有更好的方式让我在Java中发送多条短信? 我不应该有“发送消息”的方法吗?
谢谢你的帮助......我的大脑被炒了......
麦克风
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.