【发布时间】:2022-08-08 21:33:35
【问题描述】:
是否可以使用 Javascript 将表单数据发送到电报? 我阅读了很多答案,但几乎所有答案都是基于 php 的。
标签: javascript html telegram
是否可以使用 Javascript 将表单数据发送到电报? 我阅读了很多答案,但几乎所有答案都是基于 php 的。
标签: javascript html telegram
您可以使用Telegram API 发送消息。
【讨论】:
是的,您可以对电报机器人进行编程并从前端 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
对不起,我的英语不好。无论如何希望这会有所帮助:)
【讨论】: