Telegram API 使用 php 或 javascript 发送消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31213294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:28:09  来源:igfitidea点击:

Telegram API send messages with php or javascript?

javascriptphpapitelegram

提问by Eliana

How can I send messages to users via their number? I saw this site http://notificatio.divshot.io/, but there is no way to delete its references in the messages.

如何通过用户号码向用户发送消息?我看到了这个网站http://notificatio.divshot.io/,但是没有办法删除它在消息中的引用。

回答by Chris Brand

You can make use of the Telegram Bot API, it is an easy to use HTTP interface to the Telegram service. Use their BotFatherto create a bot token. JavaScript (NodeJS) usage example:

您可以使用Telegram Bot API,它是一个易于使用的 Telegram 服务的 HTTP 接口。使用他们的BotFather创建机器人令牌。JavaScript (NodeJS) 使用示例:

var TelegramBot = require('telegrambot');
var api = new TelegramBot('<YOUR TOKEN HERE>');

// You can either use getUpdates or setWebHook to retrieve updates.
// getUpdates needs to be done on an interval and will contain all the 
// latest messages send to your bot.

// Update the offset to the last receive update_id + 1
api.invoke('getUpdates', { offset: 0 }, function (err, updates) {
    if (err) throw err;
    console.log(updates);
});

// The chat_id received in the message update
api.invoke('sendMessage', { chat_id: <chat_id>, text: 'my message' }, function (err, message) {
    if (err) throw err;
    console.log(message);
});

The example makes use of a NodeJS libraryI have used for my projects.

该示例使用了我用于我的项目的NodeJS 库

In order to start a conversation with a user, you can use the deep-linking feature. For example, you can put a link on your website like so:

为了与用户开始对话,您可以使用深层链接功能。例如,您可以在您的网站上放置一个链接,如下所示:

https://telegram.me/triviabot?start=payload(payload being a custom variable value if you want to use one, like an auth ID etc)

https://telegram.me/triviabot?start=payload(payload是一个自定义变量值,如果你想使用一个,比如身份验证 ID 等)

Following that link will prompt the user to launch the Telegram App and add bot into the contact list. You will then receive a message via the getUpdates() call with the chat_id for that user. This chat_id you can then use to message the user whatever you want. I don't believe it's possible to send a message to a mobile number using the Telegram Bot API, they only work with chat_id's as this is a mechanism to protect Telegram users from being spammed by marketing bots...that is what you need to initiate the conversation with the bot first.

按照该链接将提示用户启动 Telegram 应用程序并将机器人添加到联系人列表中。然后,您将通过 getUpdates() 调用收到一条消息,其中包含该用户的 chat_id。然后您可以使用此 chat_id 向用户发送任何您想要的消息。我认为不可能使用 Telegram Bot API 向手机号码发送消息,它们只能与 chat_id 一起使用,因为这是一种保护 Telegram 用户免受营销机器人发送垃圾邮件的机制……这就是您需要的首先发起与机器人的对话。