Created
October 11, 2022 17:05
-
-
Save viveknaskar/b46d594b98598a9e41219ee54c87c8b6 to your computer and use it in GitHub Desktop.
Implementation of sending SMS using Twilio methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.twilio.rest.api.v2010.account.Message; | |
import com.twilio.type.PhoneNumber; | |
import com.viveknaskar.smsalertsystem.domain.AlertMessage; | |
import com.viveknaskar.smsalertsystem.domain.User; | |
import com.viveknaskar.smsalertsystem.service.AlertService; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class AlertServiceImpl implements AlertService { | |
@Value("${twilio.phone.number}") | |
private String phoneNumber; | |
/** | |
* This method sends SMS using the static creator() method | |
* Message.creator() has multiple implementations | |
* For our scenario, it takes 3 parameters: | |
* phoneNumber of the receiver as the first param | |
* phoneNumber of the sender as the second param (Twilio number) | |
* message (or alertMessage) as the third param | |
*/ | |
@Override | |
public void sendSMS(User user, AlertMessage alertMessage) { | |
Message.creator(new PhoneNumber(user.getPhoneNumber()), | |
new PhoneNumber(phoneNumber), | |
alertMessage.getMessage()).create(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment