Request Post API Messenger with Spring Boot

Asked

Viewed 1,439 times

2

I need to create a post method that returns this information to the Messenger API with Spring Boot, if anyone can give a light thank you.

The example below is found in the Facebook Developers documentation

curl -X POST -H "Content-Type: application/json" -d '{
  "messaging_type": "<MESSAGING_TYPE>",
  "recipient": {
    "id": "<PSID>"
  },
  "message": {
    "text": "hello, world!"
  }
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"

1 answer

1


You need the class Resttemplate, Spring Boot. It allows you to perform HTTP requests quite easily.

For this request, you need 3 things: the headers, the json with the data, the URL to call. To create json, add the following dependency to your project:

compile group: 'org.json', name: 'json', version: '20180130'

Here is the commented code:

public void post(String token) {
  String url = "https://graph.facebook.com/v2.6/me/messages? 
    access_token=".concat(token);

  //setando o header da requisição. Veja se a documentação pede algum
  //outro header além desse e adicione, se necessário
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.setContentType(MediaType.APPLICATION_JSON);

  //Montando o json esperado pelo Facebook
  JSONObject json = new JSONObject();
  json.put("messaging_type", "Algum valor aqui");

  JSONObject id = new JSONObject();
  id.put("id", "valor do ID aqui");
  json.put("recipient", id);

  JSONObject text = new JSONObject();
  text.put("text", "hello, world!");
  json.put("message", text);

  //Criando o objeto que representa a requisição a ser enviada
  HttpEntity <String> httpEntity = new HttpEntity <String> (json.toString(), httpHeaders);
  RestTemplate restTemplate = new RestTemplate();

  //Chamada propriamente dita, com a resposta do Facebook mapeada para uma String
  String response = restTemplate.postForObject(url, httpEntity, String.class);
}

Printing the json that was mounted, you have this result:

{
  "messaging_type": "Algum valor aqui",
  "recipient": {
    "id": "valor do ID aqui"
  },
  "message": {
    "text": "hello, world!"
  }
}
  • Thanks for the reply, but unfortunately it didn’t work, returning error 400 indicating that the answer is out of the standard. I am new to programming and I am having difficulties. I received a job where I need to create a Messenger bot using only Spring Boot. I’m following this example: https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start/#build My application is connecting via GET quietly, I receive BODY from messenger by a POST, but I am unable to send a POST containing a BODY of the required request along with the reply.

  • I edited the Json mount. You can try again?

  • Just thanks! Helped me a lot, gave it right here. Thanks

  • Mark the answer as correct then. It is important to help other colleagues who have the same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.