【问题标题】:Send message to Telegram through html form using Javascript使用 Javascript 通过 html 表单向 Telegram 发送消息
【发布时间】:2022-08-08 21:33:35
【问题描述】:

是否可以使用 Javascript 将表单数据发送到电报? 我阅读了很多答案,但几乎所有答案都是基于 php 的。

    标签: javascript html telegram


    【解决方案1】:

    您可以使用Telegram API 发送消息。

    【讨论】:

    【解决方案2】:

    是的,您可以对电报机器人进行编程并从前端 javascript 发送任何消息(使用 AJAX,因为电报机器人 api 是基于 Web 请求的 api)。

    例如,您通过以下方式向特定用户发送消息:

    let bot = {
        token: "BOT_TOKEN", // Your bot's token that got from @BotFather
        chat_id: "CHAT_ID" // The user's(that you want to send a message) telegram chat id
    }
    
    /**
     * By calling this function you can send message to a specific user()
     * @param {String} the text to send
     *
    */
    function sendMessage(text)
    {
        const url = `https://api.telegram.org/bot${bot.token}/sendMessage?chat_id=${bot.chat_id}&text=${text}`; // The url to request
        const xht = new XMLHttpRequest();
        xht.open("GET", url);
        xht.send();
    }
    
    // Now you can send any text(even a form data) by calling sendMessage function.
    // For example if you want to send the 'hello', you can call that function like this:
    
    sendMessage("hello");
    

    有关更多信息,请参阅电报机器人 api 文档:https://core.telegram.org/bots/api

    对不起,我的英语不好。无论如何希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2014-12-28
      • 2020-10-26
      • 2018-02-13
      • 2018-07-25
      • 1970-01-01
      • 2013-07-02
      相关资源
      最近更新 更多