向电报机器人发送消息 [英] sending message to telegram bot

查看:58
本文介绍了向电报机器人发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,

我正在尝试向电报机器人发送消息

am trying to send message to telegram bot

但事情对我不利,

我只是想归档我想做的事情.我是 jquery 和 javascript 的新手

am just trying to archive what i want to do. am new to jquery and javascript

下面是我试过的

<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
    
<script>
  $(document).ready(function () {
    $("#add_button").click(function(event) {
       Execute();
    });
    
    var fname = document.querySelector('input[name="fname"]').value;
    var country = document.querySelector('input[name="country"]').value;
    
    Message: "<html><br>| Fullname: ${fname} <br> | Country: ${country} <br></html>";

    function Execute(){
      $.ajax({
        type: 'POST',
        url: 'https://api.telegram.org/bot<token>/sendMessage?chat_id=<id>text=<message>&parse_mode=html',
        data: Message,
         
         success: function(res) {
        $('#response').text('Message sent');
  },
        error: function() {
          alert("error failed");
        }
      });
    };

  });
</script>

<input type="text" id="fname" name="fname" placeholder="fullname">
<input type="text" id="country" name="country" placeholder="country">
  <input type="button" id="add_button" value="Submit">
  <div id="response"></div>
<body>
    
</body>
</html>

推荐答案

你的脚本有几个问题:

  • 您的 标签放错了位置
  • AFAIK,您的有效负载必须经过表单编码(而不是 GET 参数)
  • 使用 ` 代替 "
  • 在 execute 方法中读取输入值,而不是在外部读取值(否则您只能在页面加载时读取一次).
  • 您不能在消息中使用
    ,请参阅 文档
  • Your <body> tag is misplaced
  • AFAIK, your payload must be form encoded (not as GET params)
  • Use ` for format strings instead of "
  • read the input values in the execute method, not outside (otherwise you read them only once on page load).
  • You cannot use <br> or <html> in the message, see the docs
<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>

<input type="text" id="fname" name="fname" placeholder="fullname">
<input type="text" id="country" name="country" placeholder="country">
<input type="button" id="add_button" value="Submit">
<div id="response"></div>

<script>
    const token = '<token>';
    const chatId = '<chat_id>';

    $(document).ready(function () {
        $("#add_button").on('click', function (event) {
            execute();
        });

        function execute() {
            const fname = document.querySelector('#fname').value;
            const country = document.querySelector('#country').value;
            const message = `Fullname: ${fname}\nCountry: ${country}`;

            $.ajax({
                type: 'POST',
                url: `https://api.telegram.org/bot${token}/sendMessage`,
                data: {
                    chat_id: chatId,
                    text: message,
                    parse_mode: 'html',
                },
                success: function (res) {
                    console.debug(res);
                    $('#response').text('Message sent');
                },
                error: function (error) {
                    console.error(error);
                    alert("error failed");
                }
            });
        }
    });
</script>
</body>
</html>


还有安全说明:

请勿在任何制作中使用此脚本!从来没有!
每个人都可以阅读和滥用您的机器人令牌.为此使用后端.

Do NOT use this script in any production! NEVER EVER!
Everyone would be able to read and abuse your bot token. Use a backend for this.

这篇关于向电报机器人发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆