gpt4 book ai didi

javascript - 从 Telegram 消息中获取 message_id - node.js

转载 作者:行者123 更新时间:2023-11-30 21:18:37 24 4
gpt4 key购买 nike

我有一个关于我目前正在使用的 Telegram 机器人的问题。我收到以下格式的用户消息:

   update { update_id: 82618016,
message:
{ message_id: 363,
from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' },
chat: { id: 22303518, first_name: 'Steve', type: 'private' },
date: 1501501753,
text: 'j' } }

当我想访问聊天的 id 时,我可以毫无问题地使用

$.message.chat.id

一旦想要获取 message_id 或 first_name,我只会得到“undefined”。

$.message.chat.first_name

$.message.message_id

有人可以帮我吗?据我所知,我正确理解了消息的结构,所以我真的不知道这里有什么问题。

非常感谢您

编辑:我在这里添加了更多我的代码:

机器人的主要代码(包括 webhook)是这样的:

initializeBot();

function initializeBot(){

const Telegram = require('telegram-node-bot');
const PingController = require('./controllers/ping');
const OtherwiseController = require('./controllers/otherwise');

const tg = new Telegram.Telegram('MY_TOKEN_IS_HERE', {
webhook: {
url: 'https://xxx.herokuapp.com',
port: process.env.PORT || 443,
host: '0.0.0.0'
}
})

tg.router.when(new Telegram.TextCommand('/ping', 'pingCommand'), new PingController())
.otherwise (new OtherwiseController());

}

当调用 OtherwiseController 时,将调用以下代码(我将其简化为要点以阐明问题。

class OtherwiseController extends Telegram.TelegramBaseController {
handle($){

console.log($.message.chat.first_name);
console.log($.message.text);
console.log($.message.chat.id);
console.log($.message.message_id);

}
}

此消息的控制台输出

update { update_id: 82618020,
message:
{ message_id: 371,
from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' },
chat: { id: 22303518, first_name: 'Steve', type: 'private' },
date: 1501509762,
text: 'hello' } }

会是:

undefined
hello
22303518
undefined

最佳答案

使用下面的方法提取你的json对象的键,然后你可以用一个合适的键访问:

Object.keys($.message.chat);

关于javascript - 从 Telegram 消息中获取 message_id - node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45415535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com