简体   繁体   English

如何接收电报Bot方法(API)的返回参数

[英]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. 我有一个JSP的本地网页,并且可以通过此页面的电报机器人API将sendMessage或sendPhoto发送给电报用户。 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. 但是从我的页面提交此请求后,URL重定向到另一个页面,该页面返回此处显示的电报Bot API方法的值。 I want to get these parameters and return values on my local page, and I don't want to go to that URL. 我想获取这些参数并在本地页面上返回值,并且我不想转到该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. https://api.telegram.org/bot<Token>/sendMessage和请求的结果会显示给我,并通过myChatId的ID向您发送问候。 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. 因此,我希望该页面不显示,并且将位于当前的本地页面上,但是我可以在本地页面上接收并查看通过此方法发送消息的结果和返回参数。

You can use an ajax request from your local page to telegram api url with post method and block page reload: 您可以使用本地方法中的ajax请求通过post方法发送电报api网址,并阻止页面重新加载:

Run in jsfiddle 在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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM