Skip to content

Let's use AI to Earn!

1.2 KB
Raw Blame
import type { DestinationStream } from 'pino'
import crypto from 'node:crypto'
import { Logger } from '@nestjs/common'

export interface FeishuOptions {
  url: string
  secret: string
}
export class FeishuLogger implements DestinationStream {
  private readonly logger = new Logger(FeishuLogger.name)

  constructor(private readonly options: FeishuOptions) {
  }

  async write(msg: string): Promise<void> {
    const timestamp = Math.floor(Date.now() / 1000)
    const sign = crypto
      .createHmac('sha256', `${timestamp}\n${this.options.secret}`)
      .digest()
      .toString('base64')

    let content: unknown
    try {
      content = JSON.parse(msg)
    }
    catch {
      content = { raw: msg }
    }

    await fetch(this.options.url, {
      method: 'POST',
      body: JSON.stringify({
        timestamp,
        sign,
        msg_type: 'text',
        content: {
          text: JSON.stringify(content, null, 2),
        },
      }),
    })
      .then(r => r.json() as Promise<{ code: number }>)
      .then((r) => {
        if (r.code !== 0) {
          this.logger.error(r, 'Feishu send failed')
        }
      })
      .catch(e => this.logger.error(e, 'Feishu request failed'))
  }
}