tgbot.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import TgBaseService from './tgbase';
  2. /**
  3. * tg机器人
  4. */
  5. export default class TgbotService extends TgBaseService {
  6. async tgTest() {
  7. return await this.reqTgbotApi('test', {});
  8. }
  9. /**
  10. * bot list
  11. */
  12. async list() {
  13. return await this.getRepo().admin.tg.TgBot.find();
  14. }
  15. /**
  16. * bot count
  17. */
  18. async count() {
  19. return await this.getRepo().admin.tg.TgBot.count();
  20. }
  21. /**
  22. * 分页加载bot
  23. */
  24. async page(page: number, count: number) {
  25. return await this.getRepo().admin.tg.TgBot.find({
  26. order: {
  27. id: 'ASC',
  28. },
  29. take: count,
  30. skip: page * count,
  31. });
  32. }
  33. /**
  34. * bot add
  35. */
  36. async add(param:any) {
  37. const { botId, username, name, token, status, description } = param;
  38. await this.getRepo().admin.tg.TgBot.insert({ botId, username, name, token, status, description });
  39. // 通知api进程创建bot监听
  40. if (status) await this.reqTgbotApi('addTgBot', botId);
  41. else await this.reqTgbotApi('removeTgBot', botId);
  42. return { botId };
  43. }
  44. /**
  45. * bot update
  46. */
  47. async update(param:any) {
  48. const { id, botId, username, name, token, status, description } = param;
  49. const bot = await this.getRepo().admin.tg.TgBot.save({ id, botId, username, name, token, status, description });
  50. // 通知api进程创建bot监听
  51. if (status) await this.reqTgbotApi('addTgBot', botId);
  52. else await this.reqTgbotApi('removeTgBot', botId);
  53. return bot;
  54. }
  55. /**
  56. * bot del
  57. */
  58. async delete(id:number) {
  59. const bot = await this.getRepo().admin.tg.TgBot.findOne({ id });
  60. if (bot != null) {
  61. await this.getRepo().admin.tg.TgBot.delete(id);
  62. await this.reqTgbotApi('removeTgBot', bot.botId);
  63. }
  64. return id;
  65. }
  66. }