V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenzhe
V2EX  ›  Telegram

NodeJS 中用来管理 Telegram Bot 的求助。

  •  
  •   chenzhe · 39 天前 · 635 次点击
    这是一个创建于 39 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写过 Bot ,勉强算是有一点经验吧,但是都是通过环境变量的方式将 Bot 的 Token 写好,然后将各种自动回复的逻辑写好,直接跑起来。 不过这次是想写一个 TG Bot 的管理面板,可以在面板中添加、删除 Bot 。我大致能想到,往数据库中插入的数据是啥样的。

    const botInfo = {
      name: "机器人名称",
      token: "机器人的 Token",
      commands: [
        {
          command: "start",
          descript: "开始命令",
        },
      ],
      hears: [
        {
          regexp: "正则",
          reply: "回复内容",
        },
      ],
    };
    

    目前 API 是用 NestJS 来写,用的比较顺手的包是 nestjs-telegraf,但是这个包需要在 module 引入的时候就传入机器人的 token 了。 不知道有没有彦祖可以指导一下,怎么动态的来添加、删除机器人。

    第 1 条附言  ·  39 天前
    import { Body, Controller, Get, Post } from '@nestjs/common';
    import { AppService } from './app.service';
    import { Pub } from './decorators/pub.decorator';
    import * as grammy from 'grammy';
    const { Bot } = grammy;
    
    @Controller()
    @Pub()
    export class AppController {
      constructor(private readonly appService: AppService) {}
    
      @Post()
      async handle(@Body('token') token: string) {
        console.log('token', token);
        const bot = new Bot(token);
        bot.command('start', (ctx) => ctx.reply('Hello!'));
        bot.on('message', (ctx) => {
          console.log(ctx.message);
        });
        bot.start();
        return true;
      }
    }
    

    当前的处理方案大致是按照这样的处理方式来做,也确实能够同时handle多个bot

    16 条回复    2024-08-09 22:21:29 +08:00
    julyclyde
        1
    julyclyde  
       39 天前
    你是打算,一个进程支持多个 bot token 么?
    那是不是只能用 webhook 模式了? getUpdates 模式好像不合适?
    chenzhe
        2
    chenzhe  
    OP
       39 天前
    @julyclyde 是的,一个进程多个 Bot ,也是打算用 webhook 模式。
    julyclyde
        3
    julyclyde  
       39 天前
    @chenzhe webhook 消息好像没有包含“发给哪个 bot”吧?
    这样的话你需要设置不同的 webhook URI 了这样才能区分到底是发给哪个 bot 的
    然后可以选两种做法:直接返回一个 json 作为应对动作,或者通过 URI 反查 token 然后发出对应的 API call
    chenzhe
        4
    chenzhe  
    OP
       39 天前
    @julyclyde 肯定是每个 bot 分别去设置 webhook 的 uri ,例如我在数据库中添加了一个 bot 信息,此时才会用 telegraf 去 new 一个 bot 事例,并且用这个实例去调用 setWebhook 的指令呀。
    shizhibuyu2023
        5
    shizhibuyu2023  
       39 天前
    用 Dynamic modules 或者手动 import 试试看呗,本质不都是模块吗
    zbinlin
        6
    zbinlin  
       39 天前
    chenzhe
        7
    chenzhe  
    OP
       39 天前
    @zbinlin 主要就是不太懂该怎么用。 我知道这边可以注入 entityManager 之类的去获取数据库里的 token 然后来注册 bot ,可是它不是在启动的时候就执行了嘛。
    julyclyde
        8
    julyclyde  
       38 天前
    @chenzhe 我没用框架,不太能理解你这种做法。呵呵
    我都是自己做 IO 的
    chenzhe
        9
    chenzhe  
    OP
       38 天前
    @julyclyde #8 自己做 IO 是什么意思,有没有例子可以分享一下?
    总是得跟接口互动嘛,即使不用 nestjs 这样的,也得配合个 express 之类的呀。
    julyclyde
        10
    julyclyde  
       38 天前
    @chenzhe requests.get getUpdates 或者用 simplehttpserver 来接收 webhook
    我是这样做的
    chenzhe
        11
    chenzhe  
    OP
       38 天前
    @julyclyde #10 那是如何动态的处理 bot 呢? 例如我今天用的是这个 bot 的 token ,然后过几天我需要添加一个新的 bot 也提供这些功能,那么你是如何在不动代码的情况下去添加这个 bot 上去呢?
    julyclyde
        12
    julyclyde  
       38 天前
    @chenzhe webhook 模式,一个 HTTP 端口下面,可以根据 URI 来区分多个 bot 吧?
    不过我还没实现这部分,只是有这个想法
    chenzhe
        13
    chenzhe  
    OP
       38 天前
    @julyclyde #12 这个是可以的,param 部分可以学习官方的借口后面的 param 传 bot 的 token ,这样就可以区分不同的 bot 了。
    目前我也是这样做的,我原以为有比较好的方案,想来学习学习的。
    不过目前没问到什么好的方案。
    julyclyde
        14
    julyclyde  
       38 天前
    @chenzhe 其实我觉得直接一个 bot 实例单独搞一个进程最简单了……编程的时候甚至都不需要考虑各 bot 之间隔离数据的问题
    chenzhe
        15
    chenzhe  
    OP
       38 天前
    @julyclyde #14 可是目前要做的就是这种功能呀。一个控制面板,然后添加 bot 的 token 进去,然后可以设置几个关键字的自动回复。
    julyclyde
        16
    julyclyde  
       38 天前
    @chenzhe 添加一个 token 你就另外启动一个进程呗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   991 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:02 · PVG 03:02 · LAX 12:02 · JFK 15:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.