![](/img/trans.png)
[英]How to read message from telegram group if the bot is not in the group but user is?
[英]how to know where user starting chat? from inside group or from inside bot directly?
我已經使用 telegraf.js 創建了電報機器人
機器人工作正常,但是如果用戶直接從機器人內部發送消息,我需要處理不同的事情,假設機器人應該重播幫助命令文檔(例如)。
問題是:
如何識別用戶開始聊天的位置? 從聊天組內部還是直接從機器人內部?
我試過
var groupInfo =await ctx.telegram.getChat()
沒有成功
我認為解決方案很簡單,但直到現在我找不到它。
提前謝謝你。
您應該查看電報文檔以了解聊天類型。 它有一個名為Type
的字段,根據文檔:
聊天類型,可以是“私人”、“群組”、“超級群組”或“頻道”
因此,在 telegraf.js 中,您可以通過以下方式檢查該字段:
bot.on('text', (ctx) => {
return ctx.reply(`Chat type is: ${ctx.message.chat.type}`)
})
在您的情況下, ctx.message.chat.type == "private"
將是私下發送給您的機器人的消息,而ctx.message.chat.type == "group"
或ctx.message.chat.type == "supergroup"
是發送到組的消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.