Serving the Bay Area since 2006
0

twilio java

create table orders ( id SERIAL UNIQUE not null PRIMARY KEY, customer_name varchar(100) not null, customer_phone_number varchar(15) not null, status varchar(15) not null default 'Ready', notification_status varchar(15) not null default 'None' );

 

String pickupMessage = "Your laundry is arriving now."; String orderId = request.getParameter("id"); int id = Integer.parseInt(orderId); Order order = repository.find(id); order.setStatus("Delivered"); order.setNotificationStatus("queued"); order = repository.update(order); String callbackUrl = request.getRequestURL().toString().replace(request.getRequestURI(), "") + "/notification/status/update?id=" + order.getId(); try { messageSender.sendSMS(order.getCustomerPhoneNumber(), pickupMessage, callbackUrl); } catch (TwilioRestException e) { e.printStackTrace(); throw new ServletException(e.getLocalizedMessage()); } response.sendRedirect("/orders"); this.appSetup = new AppSetup(); try { this.client = new TwilioRestClient(appSetup.getAccountSid(), appSetup.getAuthToken()); } catch (UndefinedEnvironmentVariableException e) { e.printStackTrace(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String pickupMessage = "Your laundry is done and on its way to you!"; String orderId = request.getParameter("id"); int id = Integer.parseInt(orderId); Order order = repository.find(id); order.setStatus("Shipped"); order.setNotificationStatus("queued"); order = repository.update(order); String callbackUrl = request.getRequestURL().toString().replace(request.getRequestURI(), "") + "/notification/status/update?id=" + order.getId(); int success = 0; try { success = messageSender.sendSMS(order.getCustomerPhoneNumber(), pickupMessage, callbackUrl); } catch (TwilioRestException e) { throw new ServletException(e.getLocalizedMessage()); } if (success == 0) { response.sendRedirect(String.format("/order?id=%d", id)); } else { throw new ServletException( String.format("An error occurred while sending the SMS. Error code: %d", success)); } } List params = new ArrayList(); params.add(new BasicNameValuePair("To", toNumber)); try { params.add(new BasicNameValuePair("From", appSetup.getTwilioNumber())); } catch (UndefinedEnvironmentVariableException e) { e.printStackTrace(); return 1; } params.add(new BasicNameValuePair("Body", messageBody)); params.add(new BasicNameValuePair("StatusCallback", callbackUrl)); MessageFactory messageFactory = client.getAccount().getMessageFactory(); messageFactory.create(params); String orderId = request.getParameter("id"); String status = request.getParameter("MessageStatus"); int id = Integer.parseInt(orderId); Order order = repository.find(id); order.setNotificationStatus(status); repository.update(order); package com.twilio.etanotifications.servlets; import com.twilio.etanotifications.lib.MessageSender; import com.twilio.etanotifications.models.Order; import com.twilio.etanotifications.repositories.OrdersRepository; import com.twilio.sdk.TwilioRestException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class NotificationStatusServlet extends HttpServlet { private final OrdersRepository repository; @SuppressWarnings("unused") public NotificationStatusServlet() { this(new OrdersRepository()); } public NotificationStatusServlet(OrdersRepository repository) { this.repository = repository; } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String orderId = request.getParameter("id"); String status = request.getParameter("MessageStatus"); int id = Integer.parseInt(orderId); Order order = repository.find(id); order.setNotificationStatus(status); repository.update(order); } }
.hidden { display: none; }