TypeError:无法读取未定义的属性“聊天”

时间:2019-05-09 17:33:59

标签: node.js express

我在为Telegram机器人编写的My Expressjs代码上遇到此错误 我使用Nginx在CloudVps上托管了此代码 我将其用于Telegram Bot API webhook

这是电报聊天机器人 在用户输入匹配时设置一些自动回复,它将发送自动回复。

日志文件错误

TypeError: Cannot read property 'chat' of undefined
    at app.post (/var/www/hellobot/index.js:22:37)
    at Layer.handle [as handle_request] (/var/www/hellobot/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/hellobot/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/www/hellobot/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/www/hellobot/node_modules/express/lib/router/layer.js:95:5)
    at /var/www/hellobot/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/var/www/hellobot/node_modules/express/lib/router/index.js:335:12)
    at next (/var/www/hellobot/node_modules/express/lib/router/index.js:275:10)
    at /var/www/hellobot/node_modules/body-parser/lib/read.js:130:5
    at invokeCallback (/var/www/hellobot/node_modules/raw-body/index.js:224:16)

这是我的代码

app.listen(port, () => {
    console.log(`Listening on port ${port}`);
});

app.post('/', (req, res, next) => {
    var Content = require('./blogposts.json');
    var random = Content.blogtext[Math.floor(Math.random() * Content.blogtext.length)];
    const chatId = req.body.message.chat.id;
    const sentMessage = req.body.message.text || req.body.message.sticker.file_id;
    if (sentMessage.match(/start/gi)) {
        axios.post(`${url}${apiToken}/sendMessage`, {
                chat_id: chatId,
                text: 'Hello I am Bot',
            })
            .then((response) => {
                next();
                res.status(200).send(response);
                return res.end();
            }).catch((error) => {
                res.send(error);
            });
    } else if (sentMessage.match(/posts/gi)) {
        axios.post(`${url}${apiToken}/sendMessage`, {
                chat_id: chatId,
                text: random.blogtext,
            })
            .then((response) => {
                next();
                res.status(200).send(response);
            }).catch((error) => {
                res.send(error);
            });
    } else {
        axios.post(`${url}${apiToken}/sendMessage`, {
                chat_id: chatId,
                text: 'Sorry answer not found',
            })
            .then((response) => {
                res.status(200).send(response);
            }).catch((error) => {
                res.send(error);
            });
    }
});

0 个答案:

没有答案
相关问题