nandenjin/gmail-to-slack

View on GitHub
src/slackUtil.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { IncomingWebhookSendArguments } from '@slack/webhook'
import { props } from './props'

export namespace slackUtil {
  export function prepareEmailFrom(from: string) {
    // Truncate if the length of from is too long
    // todo - Preserve domain or whole email address if possible
    if (from.length > 50) {
      const postfix = '...'
      from = from.substring(0, 50 - postfix.length) + postfix
    }

    return from
  }

  export function prepareEmailBody(body: string): string {
    const originalLines = body
      // Truncate original messages
      .replace(/\n-+\s*Original message\s*-+\n[\s\S]*$/i, '')
      .replace(/\n\d{4}.+? \d{1,2}:\d{2} .+?<.+?@.+?>:\s+>[\s\S]*$/i, '')

      // Remove spaces at the end of body
      .replace(/\s*$/, '')

      // Remove multiple line-breaks
      .replace(/\n{3,}/g, '\n\n')

      .split('\n')

    const resultLines: string[] = []
    /** Virtual length of lines */
    let len = 0
    for (const line of originalLines) {
      len += Math.ceil(line.length / 30) // Treat 30 characters as one line
      resultLines.push(line)

      // Truncate if the length of lines is too long
      if (len > 15) {
        resultLines.push('(...)')
        break
      }
    }
    return resultLines.join('\n')
  }

  /**
   * Compose message for Slack
   * @param from Sender of the email
   * @param subject Subject of the email
   * @param body Body of the email
   */
  export function composeMessage(
    from: string,
    subject: string,
    body: string
  ): IncomingWebhookSendArguments {
    return {
      username: prepareEmailFrom(from),
      text: '',
      attachments: [
        {
          title: subject,
          text: prepareEmailBody(body),
        },
      ],
    }
  }

  /** Post message to Slack by Incoming Webhook.
   * @param msg Argument for Incoming Webhook
   */
  export function postMessage(msg: IncomingWebhookSendArguments): void {
    const url = props.getSlackUrl()
    const response = UrlFetchApp.fetch(url, {
      method: 'post',
      payload: 'payload=' + encodeURIComponent(JSON.stringify(msg)),
      muteHttpExceptions: true,
    })

    const responseCode = response.getResponseCode()
    if (200 <= responseCode && responseCode < 300) {
      console.log('Successfly forwarded.')
    } else {
      console.log('Payload: ', msg)
      console.error('Response: ', response.getContentText())
      throw new Error('Webhook responded with error code:' + responseCode)
    }
  }
}