如何使用node.js node-telegram-bot-api订阅和订阅用户订阅用户?

发布于 2025-02-03 01:38:50 字数 708 浏览 3 评论 0原文

我正在编写一个程序来使用node.js和node-telegram-bot-api模块开发电报聊天机器人。我想设置命令,以便在接收 /取消订阅后,应从机器人中取消订阅。我阅读了该模块的GitHub文档,但找不到实现此目的的方法。 https://github.com/yagop/ Node-Telegram-bot-api/blob/master/doc/api.md

在此DOC中,有一个方法DeleteChat可用,但是我不确定它是否可以与模块一起使用。 https://core..telegram.telegram.telegram.telegram.org/methods#working-working-working-working-working-working-working-working-workssssssssssssupergroupsschannels

感谢任何建议或帮助,谢谢!

I'm writing a program to develop a telegram chatbot using Node.js and node-telegram-bot-api module. I want to set command such that when received /unsubscribe the user should be unsubscribed from the bot. I read the github document for the module but couldn't find a method to achieve this.
https://github.com/yagop/node-telegram-bot-api/blob/master/doc/api.md

In this doc, there's a method deleteChat available, but I'm not sure it will work with the module.
https://core.telegram.org/methods#working-with-chatssupergroupschannels

Any advice or help is appreciated, Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2025-02-10 01:38:50

您需要一个数据库将用户设置为“订阅”或“未取消”

。软件包/存储“ rel =“ nofollow noreferrer”>商店软件包
您将需要以下包:

  • 存储
  • 节点telegram-bot-api

商店软件包是本地存储,当应用重新启动

下面给出的代码时,可能会重置您的想法,它只能创建订阅和退出的功能,您可以使用循环方法将新闻发送给“数组”中的用户
const TelegramBot = require('node-telegram-bot-api');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
var store = require('store')
var subscribed_users = store.get('subscribed')
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});

// Matches "/echo [whatever]"
bot.onText('/start', (msg) => {
  // 'msg' is the received Message from Telegram
const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Welcome!\nUser /subscribe to subscribe to newsletter or /unsubscribe to unsubscribe to news letter');
});
bot.onText('/subscribe', (msg) => {
  if(!susbcribed_users){
  var subscribed_users = []
}
  const chatId = msg.chat.id;
  //retrive subscribed users array data
if(subscribed_users.includes(msg.chat.id)){
    bot.sendMessage(chatId, "You're already subscribed to newsletter!")
    return
}
var new_data = subscribed_users.push(msg.chat.id)
store.set('subscribed', new_data)
  bot.sendMessage(chatId, "You're subscribed to newsletter!")
 
})

bot.inText('/unsubscribe', (msg) => {
  const chatId = msg.chat.id;
  if(!subscribed_users.includes(msg.chat.id)){
    bot.sendMessage(chatId, "You're not subscribed to newsletter!")
    return
}
  const newArr = arr.filter(object => {
  return object !== msg.chat.id;
});
store.set('subscribed', newArr)
bot.sendMessage(chatId, "You're subscribed to newsletter!")
})

You'd need a database to set user as 'subscribed' or 'unsubscribed' for that.

I'll give you an example with store package
You'll need the following packages:

  • store
  • node-telegram-bot-api

store package is local storage, may reset when the app restarts

The code given below may give you an idea, it just enables you to create the functionality of Subscribe and Unsubscribe, you may use loop methods to send news to users in 'array'
const TelegramBot = require('node-telegram-bot-api');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
var store = require('store')
var subscribed_users = store.get('subscribed')
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});

// Matches "/echo [whatever]"
bot.onText('/start', (msg) => {
  // 'msg' is the received Message from Telegram
const chatId = msg.chat.id;
  bot.sendMessage(chatId, 'Welcome!\nUser /subscribe to subscribe to newsletter or /unsubscribe to unsubscribe to news letter');
});
bot.onText('/subscribe', (msg) => {
  if(!susbcribed_users){
  var subscribed_users = []
}
  const chatId = msg.chat.id;
  //retrive subscribed users array data
if(subscribed_users.includes(msg.chat.id)){
    bot.sendMessage(chatId, "You're already subscribed to newsletter!")
    return
}
var new_data = subscribed_users.push(msg.chat.id)
store.set('subscribed', new_data)
  bot.sendMessage(chatId, "You're subscribed to newsletter!")
 
})

bot.inText('/unsubscribe', (msg) => {
  const chatId = msg.chat.id;
  if(!subscribed_users.includes(msg.chat.id)){
    bot.sendMessage(chatId, "You're not subscribed to newsletter!")
    return
}
  const newArr = arr.filter(object => {
  return object !== msg.chat.id;
});
store.set('subscribed', newArr)
bot.sendMessage(chatId, "You're subscribed to newsletter!")
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文