0

Send the error to Telegram

by
Published Apr 21, 2025

Sends the error to a Telegram bot

Scriptยท failure telegram
  • Submitted by nick kadutskyi374 Bun
    Created 415 days ago
    1
    import * as wmill from "windmill-client"
    2
    import { Telegraf } from "telegraf";
    3
    import { FmtString } from "telegraf/format";
    4
    type Telegram = {
    5
      token: string
    6
    }
    7
    type CTelegramChat = {
    8
      chat_id: string
    9
    }
    10
    export async function main(
    11
      message: string, 
    12
      name: string, 
    13
      step_id: string, 
    14
      auth: Telegram,
    15
      telegramChat: CTelegramChat,
    16
    ) {
    17
      const bot = new Telegraf(auth.token)
    18
    
    
    19
      const baseURL = process.env.WM_BASE_URL
    20
      const workspace = process.env.WM_WORKSPACE
    21
    
    
    22
      const rootFlowId = process.env.WM_ROOT_FLOW_JOB_ID
    23
      const flowPath = process.env.WM_FLOW_PATH
    24
      const rootFlowURL = `${baseURL}/run/${rootFlowId}?workspace=${workspace}`
    25
    
    
    26
      const schedulePath = process.env.WM_SCHEDULE_PATH
    27
    
    
    28
    
    
    29
      let text = "";
    30
      if (rootFlowId){
    31
        const flow = await wmill.FlowService
    32
          .getFlowByPath({workspace, path: flowPath})
    33
          .catch(() => null)
    34
        const flowModule = flow?.value?.modules?.find(m => m.id === step_id)
    35
    
    
    36
        text += `Step <b>[${step_id}]`
    37
    
    
    38
        if (flowModule){
    39
          text += ` ${flowModule.summary}`;
    40
        } 
    41
        text += `</b>`
    42
    
    
    43
        if (flowModule?.value?.path){
    44
          const scriptPath = flowModule?.value?.path;
    45
          const script = await wmill.ScriptService
    46
            .getScriptByPath({path: scriptPath, workspace})
    47
            .catch(() => null);
    48
    
    
    49
          if (script){
    50
            const scriptURL = `https://windmill.kdtsk.com/scripts/get/${script.hash}?workspace=${workspace}`
    51
            text += ` (<a href="${scriptURL}">${scriptPath}</a>)`;
    52
          } else {
    53
            text += ` (${scriptPath})`;
    54
          }
    55
        }
    56
    
    
    57
        text += ` in\nFlow <a href="${rootFlowURL}">${flowPath}</a> `;
    58
      } 
    59
    
    
    60
      if (schedulePath) {
    61
        text += `triggered by ${schedulePath} schedule `
    62
      }
    63
    
    
    64
      text += `\nhad an error:\n<pre><code class="language-${name}">${message}</code></pre>`;
    65
    
    
    66
      const res = await bot.telegram.sendMessage(telegramChat.chat_id, new FmtString(text), {
    67
        parse_mode: "HTML",
    68
      })
    69
      
    70
      return {res};
    71
    }
    72
    
    
    73