node-telegram-bot-api 中的用户数据库逻辑

User database logic in node-telegram-bot-api

提问人:Anton 提问时间:11/16/2023 最后编辑:lemAnton 更新时间:11/16/2023 访问量:47

问:

我正在为我们学院的学生制作带有 Firebase 的 tg 机器人,当我们的家庭作业准备好时,它会从学习管理系统发送通知。使用 Puppeteer 为 LMS 制作解析器并将其推送到 Firebase 非常容易,但对我来说很难理解 Firebase 的工作原理。

在Firebase中,我有一个数据库,其中包含两个集合,即Assignments和Users,目前只有Assignments集合。

class Firebase {
  static id = 0;

  constructor(config) {
    const app = initializeApp(config.firebaseConfig.firebase);
    this.db = getDatabase(app);
    this.config = config;
  }

  async auth() {
    try {
      const auth = getAuth();
      const user = await signInWithEmailAndPassword(
        auth,
        this.config.firebaseConfig.auth.email,
        this.config.firebaseConfig.auth.password
      );
      return user;
    } catch (err) {
      throw new Error('User not found!');
    }
  }

  async get(key) {
    const dbRef = ref(getDatabase());
    try {
      const snapshot = await get(child(dbRef, key));
      if (!snapshot.exists()) {
        throw new Error('No data available!');
      }
      return snapshot.val();
    } catch (err) {
      return err;
    }
  }

  set(data, key) {
    set(ref(this.db, `${key}/${id}`), data);
  }

  push(data, key) {
    return push(ref(this.db, key), data).key;
  }

  update(data, key, id) {
    update(ref(this.db, `${key}/${id}`), data);
  }

  remove(key, id) {
    remove(ref(this.db, `${key}/${id}`));
  }

  async getId(data, key) {
    const dbData = await this.get(key);
    const sortedData = sortKeys(data);
    return Object.entries(dbData).find(
      ([, value]) => JSON.stringify(value) === JSON.stringify(sortedData)
    )[0];
  }
}

export default Firebase;

这是我的 OOP 风格的类,用于 Puppeteer 和机器人,但我不确定和方法。所以,正如我所理解的 node-telegram-bot-api,让事情正常工作的唯一方法是这样的:setremove

const { token } = config;

const firebase = new Firebase(config);
await firebase.auth();

const bot = new TelegramBot(token, {polling: true});

const { prompts, buttons } = telegramTemplates;
const restartSyncTriggers = buttons.start.concat(buttons.restart);

bot.on ('message', async msg => {
  const coll = firebase.get(id);
  const { text, chat }  = msg;  
  const { id } = chat;
  
  if (text === '/start') {
    const startOptions = { parse_mode: "Markdown", disable_web_page_preview: true };
    const startKeyboard = new Keyboard(buttons.start, startOptions);
    firebase.push(id, 'users');
    await bot.sendMessage(id, prompts.start, startKeyboard);
  }; /* Welcome message & adding users tg id to database */

  if (text === '/restart') {
    const restartKeyboard = new Keyboard(buttons.restart);
 
    //Removing whole user from 'Users' Collection
    //Pushing user with same tg id to 'Users' Collection
    //How i think that will work:

    const coll = firebase.get(id);
    firebase.remove('users', id);
    firebase.push('users', id);

    await bot.sendMessage(id, prompts.restart, restartKeyboard);
  } /* Deleting & Creating new User to '/restart' command */

  if (restartSyncTriggers.includes(text)) {
    const courseKeyboard = new Keyboard(buttons.course)
    /* Asking user question for next info */
    await bot.sendMessage(id, prompts.course, courseKeyboard);
  };


  if (buttons.course.includes(text)) {
    if (text === buttons.course[0]) {
      //Set key 'Grade' to 1 if User typed string from keyboard
    } else if (text === buttons.course[1]) {
      //Set key 'Grade' to 2 if User typed another string from keyboard
    }
    const secondDiplomaKeyboard = new Keyboard(buttons.secondDiploma);

    await bot.sendMessage(id, prompts.secondDiploma, secondDiplomaKeyboard);
  }; /* Grade sync */

  if (buttons.secondDiploma.includes(text)) {
    if (text === buttons.secondDiploma[0]) {
      // another push to db
    } else if (text === buttons.secondDiploma[1]) {
      // another push to db
    }
    const registrationKeyboard = new Keyboard(buttons.registration);
    
    await bot.sendMessage(id, prompts.registration, registrationKeyboard);
  }; /* SecondDiploma sync*/

  if(buttons.registration.includes(text)) {
    if(text === buttons.registration[1]) {
      // another push to db
      bot.sendMessage(id, prompts.restart, buttons.restart)
    } else if (text === buttons.registration[0]) {
      // another push to db
      bot.sendMessage(id, prompts.finish, buttons.finish[0])
    }
  } /* Are you sure? Y- go to notify schedule, otherwise /restart*/
});

在每个 if 语句中都有一个脚本,该脚本会触发将该内容同步到 DB,我这样做是因为我认为发送给机器人的每条消息的所有上下文都搞砸了,所以我必须推送一个 USER 的每个键值,而不是将所有对收集到一个 USER 对象, 并将其推送到 Firebase。如果我选择 Telegraf 而不是那个 API,也许会更容易,因为 YouTube 上大多数关于 Telegraf 的相关指南视频?

我做对了吗?

我试过寻找所有写在 node-telegram-bot-api 上的 tg 机器人,但我还没有找到任何解决方案。它们都对每件事都使用相同的 if 语句,或者它们类似于天气应用程序,当您在某个城市输入时。我很迷茫。

节点.js 火库 节点电报机器人API

评论


答: 暂无答案