tailchat icon indicating copy to clipboard operation
tailchat copied to clipboard

机器人无法接收数据 || Robot cannot receive data

Open oiov opened this issue 1 year ago • 5 comments

使用官方文档代码创建的机器人:

router.post("/callback", async (ctx) => {
  console.log(ctx);
  const type = ctx.body.type;
  ...
}

我增加了一行日志,在频道里@机器人成功触发了callback,日志:

Login...
Server is running on http://localhost:3030
tailchat openapp login success!
{
request: {
method: 'POST',
url: '/callback',
header: {
host: 'tailbot.vmail.dev:443',
'x-real-ip': 'x.x.x.x',
'x-forwarded-for': ''x.x.x.x',
'remote-host': ''x.x.x.x',
'x-host': 'tailbot.vmail.dev:443',
'x-scheme': 'https',
connection: 'upgrade',
'content-length': '461',
'user-agent': 'got (https://github.com/sindresorhus/got)',
'x-tc-payload-type': 'inbox',
'content-type': 'application/json',
'accept-encoding': 'gzip, deflate, br'
}
},
response: {
status: 404,
message: 'Not Found',
header: [Object: null prototype] {}
},
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/callback',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}

TypeError: Cannot read properties of undefined (reading 'type')
at /www/wwwroot/tailchat-bot/index.js:23:25
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:42:32)
at /www/wwwroot/tailchat-bot/node_modules/koa-router/lib/router.js:423:16
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:42:32)
at /www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:34:12
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-router/lib/router.js:428:31)
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:42:32)
at /www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:34:12
at Application.handleRequest (/www/wwwroot/tailchat-bot/node_modules/koa/lib/application.js:186:12)
at Server.handleRequest (/www/wwwroot/tailchat-bot/node_modules/koa/lib/application.js:157:21)

补充: 发现请求体中没有数据所以拿不到。机器人的端口是 http://localhost:3030 我用 tailbot.vmail.dev 做了反代,在机器人的消息回调地址那里就填的是https://tailbot.vmail.dev/callback

但是我用tailchat-laf-robot可以正常拿到数据返回,host那些应该配置没问题,难道自部署是有什么差异吗?为什么ctx里没有payload?


Bot created using Official Documentation code:

router.post("/callback", async (ctx) => {
  console.log(ctx);
  const type = ctx.body.type;
  ...
}

I added a line of log. @Robot successfully triggered the callback in the channel. The log:

Login...
Server is running on http://localhost:3030
tailchat openapp login success!
{
request: {
method: 'POST',
url: '/callback',
header: {
host: 'tailbot.vmail.dev:443',
'x-real-ip': 'x.x.x.x',
'x-forwarded-for': ''x.x.x.x',
'remote-host': ''x.x.x.x',
'x-host': 'tailbot.vmail.dev:443',
'x-scheme': 'https',
connection: 'upgrade',
'content-length': '461',
'user-agent': 'got (https://github.com/sindresorhus/got)',
'x-tc-payload-type': 'inbox',
'content-type': 'application/json',
'accept-encoding': 'gzip, deflate, br'
}
},
response: {
status: 404,
message: 'Not Found',
header: [Object: null prototype] {}
},
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/callback',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}

TypeError: Cannot read properties of undefined (reading 'type')
at /www/wwwroot/tailchat-bot/index.js:23:25
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:42:32)
at /www/wwwroot/tailchat-bot/node_modules/koa-router/lib/router.js:423:16
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:42:32)
at /www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:34:12
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-router/lib/router.js:428:31)
at dispatch (/www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:42:32)
at /www/wwwroot/tailchat-bot/node_modules/koa-compose/index.js:34:12
at Application.handleRequest (/www/wwwroot/tailchat-bot/node_modules/koa/lib/application.js:186:12)
at Server.handleRequest (/www/wwwroot/tailchat-bot/node_modules/koa/lib/application.js:157:21)

Replenish: It was found that there was no data in the request body so it could not be obtained. The robot's port is http://localhost:3030. I used tailbot.vmail.dev for reverse generation. The message callback address of the robot is https://tailbot.vmail.dev/callback'. .

But I can use tailchat-laf-robot to get the data back normally. The host should be configured with no problem. Is it self-deployment? Is there any difference? Why is there no payload in ctx?

oiov avatar May 07 '24 13:05 oiov

image

Looks here has a 404 error

moonrailgun avatar May 07 '24 13:05 moonrailgun

我也不知道为什么是404,这是我所有代码:

const { TailchatHTTPClient, stripMentionTag } = require("tailchat-client-sdk");
const Koa = require("koa");
const Router = require("koa-router");

const app = new Koa();
const router = new Router();

const host = "https://im.vmail.dev";
const appId = "---";
const appSecret = "---";

const client = new TailchatHTTPClient(host, appId, appSecret);

// 定义路由
router.get("/", async (ctx) => {
  ctx.body = "Hello, World!";
});

router.post("/callback", async (ctx) => {
  console.log(ctx);
  const type = ctx.body.type;

  if (type === "message") {
    const payload = ctx.body.payload;
    try {
      const message = await client.replyMessage(
        {
          messageId: payload.messageId,
          author: payload.messageAuthor,
          content: payload.messageSnippet,
        },
        {
          groupId: payload.groupId,
          converseId: payload.converseId,
          content: `Your message: ${stripMentionTag(payload.messageSnippet)}`,
        }
      );

      console.log("send message success:", message);
    } catch (err) {
      console.log("send message failed:", err);
    }
  }

  ctx.body = "Bot Callback Page";
});

// 注册路由中间件
app.use(router.routes());
app.use(router.allowedMethods());

// 启动服务
app.listen(3030, () => {
  console.log("Server is running on http://localhost:3030");
});

oiov avatar May 07 '24 13:05 oiov

you can try to send a post request by yourself at first rather than tailchat.

moonrailgun avatar May 07 '24 14:05 moonrailgun

you can try to send a post request by yourself at first rather than tailchat.

I send a post request to https://tailbot.vmail.dev/callback , no request body set, and error log similar to tailchat:

{
request: {
method: 'POST',
url: '/callback',
header: {
host: 'tailbot.vmail.dev:443',
'x-real-ip': 'x.x.x.x',
'x-forwarded-for': 'x.x.x.x',
'remote-host': 'x.x.x.x',
'x-host': 'tailbot.vmail.dev:443',
'x-scheme': 'https',
connection: 'upgrade',
'content-length': '0',
'user-agent': 'PostmanRuntime/7.38.0',
accept: '*/*',
'cache-control': 'no-cache',
'postman-token': 'fd4a2774-cea3-4053-bfcb-bb8b555d75fb',
'accept-encoding': 'gzip, deflate, br'
}
},
response: {
status: 404,
message: 'Not Found',
header: [Object: null prototype] {}
},
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/callback',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}

oiov avatar May 09 '24 06:05 oiov

looks like your node server not work correct. looks not tailchat's problem can you try to visit by localhost rather then domain?

moonrailgun avatar May 09 '24 13:05 moonrailgun

looks like your node server not work correct. looks not tailchat's problem can you try to visit by localhost rather then domain?

请问是这样吗?但我似乎无法触发callback {6EF717B5-9B79-4247-A377-CC9FCBF1D6A5} {D1CEE737-8940-448a-A07E-0C0E049D7502}

oiov avatar May 17 '24 08:05 oiov

can you try to trigger request manual first?

moonrailgun avatar May 17 '24 11:05 moonrailgun

is your tailchat deploy in local?

moonrailgun avatar May 17 '24 11:05 moonrailgun

is your tailchat deploy in local?

I deployed docker to my server.

oiov avatar May 18 '24 03:05 oiov