简体   繁体   English

使用 Javascript 通过 html 表单向 Telegram 发送消息

[英]Send message to Telegram through html form using Javascript

Is it possible to send form data to telegram using Javascript?是否可以使用 Javascript 将表单数据发送到电报? I read many answers but almost all are php based.我阅读了很多答案,但几乎所有答案都是基于 php 的。

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

Yes you can program your telegram bot and send any message using javascript (using AJAX since the telegram bot api is a web request based api). Yes you can program your telegram bot and send any message using javascript (using AJAX since the telegram bot api is a web request based api).

For example, you send message to a specific user by this:例如,您通过以下方式向特定用户发送消息:

let tg = {
    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${tg.token}/sendMessage?chat_id=${tg.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");

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

Sorry for my bad English.对不起,我的英语不好。 Anyway, I hope this helps:)无论如何,我希望这会有所帮助:)

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

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