Giter Club home page Giter Club logo

sms's Introduction

SMS

package com.twilio.websocketcallback.domain;

public class SMS { private String to; private String message;

public String getTo() {
    return to;
}

public void setTo(String to) {
    this.to = to;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}


@Override
public String toString() {
    return "SMS{" +
            "to='" + to + '\'' +
            ", message='" + message + '\'' +
            '}';
}

} Criar um controlador de gerenciamento de mensagens Na abordagem do Spring para trabalhar com mensagens STOMP, as mensagens STOMP podem ser roteadas para classes @Controller. Por exemplo, SMSController eฬ mapeado para lidar com mensagens para o destino /sms.

package com.example.websocketcallback;

import com.twilio.exception.ApiException; import com.twilio.websocketcallback.domain.SMS; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;

@RestController public class SMSController {

@Autowired
SMSService service;

@Autowired
private SimpMessagingTemplate webSocket;

private final String  TOPIC_DESTINATION = "/topic/sms";

@RequestMapping(value = "/sms", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void smsSubmit(@RequestBody SMS sms) {
    try{
        service.send(sms);
    }
    catch(ApiException e){

        webSocket.convertAndSend(TOPIC_DESTINATION, getTimeStamp() + ": Error sending the SMS: "+e.getMessage());
        throw e;
    }
    webSocket.convertAndSend(TOPIC_DESTINATION, getTimeStamp() + ": SMS has been sent!: "+sms.getTo());

}

@RequestMapping(value = "/smscallback", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void smsCallback(@RequestBody MultiValueMap<String, String> map) {
   service.receive(map);
   webSocket.convertAndSend(TOPIC_DESTINATION, getTimeStamp() + ": Twilio has made a callback request! Here are the contents: "+map.toString());
}

private String getTimeStamp() {
   return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
}

}

sms's People

Contributors

analistadeperon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.