Atefeh Rashidi
Atefeh Rashidi

Reputation: 515

How to receive return parameters of a telegram bot method (API)

I have a local web page by JSP, and I can sendMessage or sendPhoto to a telegram user by the telegram robot API through this page. But after submitting this request from my page, the URL redirects to another page that return values of the telegram Bot API method shown there. I want to get these parameters and return values on my local page, and I don't want to go to that URL.

This is for example:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head></head>

    <body>
        <form action="https://api.telegram.org/bot<token>/sendMessage" method="POST">
            <input type="text" name="chat_id" id="chat_id"value="myChatId">
            <input type="text" name="text" id="text" value="hello">
            <input type="submit" value="submit">
        </form>
    </body>
</html>

When I click submit this page appears:

https://api.telegram.org/bot<Token>/sendMessage and results of the request is shown to me and hello is send to the user by the Id of myChatId. So, I want this page does not show, and I will be on my current local page, but I can receive and see results and return parameters of sending the message by this method on my local page.

Upvotes: 1

Views: 4658

Answers (1)

Sadegh PM
Sadegh PM

Reputation: 452

You can use an ajax request from your local page to telegram api url with post method and block page reload:

Run in jsfiddle

$('form').submit(function(e) {
  e.preventDefault();
  $.ajax({
    url: 'https://api.telegram.org/bot' + $('#token').val() + '/sendMessage',
    method: 'POST',
    data: {
      chat_id: $('#chat_id').val(),
      text: $('#text').val()
    },
    success: function() {
      alert('your message has been sent!');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>
  <input type="text" name='token' id="token" placeholder="your bot token">
  <input type="text" name="chat_id" id="chat_id" value="myChatId">
  <input type="text" name="text" id="text" value="hello">
  <input type="submit" value="submit">
</form>

Upvotes: 3

Related Questions