Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Created October 11, 2022 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viveknaskar/b46d594b98598a9e41219ee54c87c8b6 to your computer and use it in GitHub Desktop.
Save viveknaskar/b46d594b98598a9e41219ee54c87c8b6 to your computer and use it in GitHub Desktop.
Implementation of sending SMS using Twilio methods
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