package com.example.demo;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
/**
* @author seif
* @Description: TODO
* @date 2018/7/2719:46
* @Modified by:
*/
@Service
public class TsmsService {
final Logger logger = LogManager.getLogger(TsmsService.class);
private static boolean init;
private static String TWILIO_SID;
private static String TWILIO_TOKEN;
private static String TWILIO_FROMPHONE;
@Value("${sms.TWILIO_SID}")
public void setTwilioSid(String twilioSid) {
TWILIO_SID = twilioSid;
}
@Value("${sms.TWILIO_TOKEN}")
public void setTwilioToken(String TWILIO_Token) {
TWILIO_TOKEN = TWILIO_Token;
}
@Value("${sms.TWILIO_FROMPHONE}")
public void setTwilioFromPhone(String TWILIO_FromPhone) {
TWILIO_FROMPHONE = TWILIO_FromPhone;
}
@PostConstruct
public void init() {
try {
Twilio.init(TWILIO_SID, TWILIO_TOKEN);
logger.warn("===TWILIO init sucessfull");
} catch (Exception e) {
logger.error("===TWILIO init failed");
e.printStackTrace();
}
}
public boolean send2TSMS(String toPhone, String msg) {
if (toPhone.contains("_")) {
toPhone = StringUtils.replace(toPhone, "_", "");
}
if (!toPhone.startsWith("+")) {
toPhone = "+" + toPhone;
}
Message message = Message.creator(new PhoneNumber(toPhone), new PhoneNumber(TWILIO_FROMPHONE), msg).create();
if (message != null && message.getSid() != null) return true;
return false;
}
}
compile group: 'com.twilio.sdk', name: 'twilio', version: '7.21.7'