Telegram API send messages with php or javascript? [closed]

淺唱寂寞╮ 提交于 2019-12-13 05:00:43

问题


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.


回答1:


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

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 (payload being a custom variable value if you want to use one, like an auth ID etc)

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.



来源:https://stackoverflow.com/questions/31213294/telegram-api-send-messages-with-php-or-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!