skip to Main Content

I’m learning how to make telegram bots. With webhook telegram send you a post request with some message.
I want my server to get this request, process and send new request to telegram get/post.
For example:
telegram bot sent me a post request with a new message in chat.
i get this request and proccess it.
now i need to send new get request to telegram to post a message like https://api.telegram.org/bot/sendMessage?chat_id=&text=hello

Is there a way to send request directly from controller? I know that i can redirect request, but redirect can be only GET and i need completly new request.

2

Answers


  1. you can use Rest Template to send request to a web service.this is a spring guide on how to do it.

    Login or Signup to reply.
    1. You can either use Spring’s RestTemplate:

      RestTemplate restTemplate = new RestTemplate();
      
      UriComponentsBuilder telegramRequestBuilder = UriComponentsBuilder.fromHttpUrl("https://api.telegram.org/bot/sendMessage")
              .queryParam("chat_id", 1)
              .queryParam("text", "Hello");
      
      ResponseEntity<String> response
              = restTemplate.getForEntity(telegramRequestBuilder.toUriString(), String.class); // or to a Java pojo class
      
    2. Or use the newer Spring WebClient for this. See this link for example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search