Telegram API 使用 php 或 javascript 发送消息? [英] Telegram API send messages with php or javascript?

查看:80
本文介绍了Telegram API 使用 php 或 javascript 发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

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);
});

该示例使用了我在项目中使用的 NodeJS 库.

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

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

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(有效载荷是自定义的如果你想使用一个变量值,比如身份验证 ID 等)

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

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

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 API 使用 php 或 javascript 发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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