Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
oiov opened this issue May 7, 2024 · 10 comments
Closed

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

oiov opened this issue May 7, 2024 · 10 comments

Comments

@oiov
Copy link

oiov commented May 7, 2024

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

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?

@github-actions github-actions bot changed the title 机器人无法接收数据 机器人无法接收数据 || Robot cannot receive data May 7, 2024
@moonrailgun
Copy link
Contributor

image

Looks here has a 404 error

@oiov
Copy link
Author

oiov commented May 7, 2024

我也不知道为什么是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");
});

@moonrailgun
Copy link
Contributor

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

@oiov
Copy link
Author

oiov commented May 9, 2024

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>'
}

@moonrailgun
Copy link
Contributor

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

@oiov
Copy link
Author

oiov commented May 17, 2024

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}

@moonrailgun
Copy link
Contributor

can you try to trigger request manual first?

@moonrailgun
Copy link
Contributor

moonrailgun commented May 17, 2024

is your tailchat deploy in local?

@oiov
Copy link
Author

oiov commented May 18, 2024

is your tailchat deploy in local?

I deployed docker to my server.

@oiov oiov closed this as completed May 24, 2024
@YInJunWen
Copy link

YInJunWen commented Nov 29, 2024

我遇到了和你一样的问题。问题的根本原因是Koa.js在解析JSON数据时需要一个中间件来处理请求体(body)的内容。如果你不能解决这个问题,我建议你使用Express框架。Express的旧版本与Koa.js有同样的问题,但是在最新的版本中,它们已经将解决方案内置了。因此,你可以非常容易地使用它,并且能够获取到机器人被@之后的信息。

I encountered the same issue as you. The root cause is that Koa.js requires a middleware to handle the body content when parsing JSON data. If you're unable to resolve this, I recommend using the Express framework. Older versions of Express had the same problem as Koa.js, but in the latest version, they have integrated the solution internally. Therefore, you can use it quite easily and obtain the information after the bot is @-mentioned.

router.post("/bot/callback", async (req, res) => {
  const type = req.body.type;

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants