Edits history of script submission #13960 for ' Send the error to Telegram (telegram)'

  • bun
    import * as wmill from "windmill-client"
    import { Telegraf } from "telegraf";
    import { FmtString } from "telegraf/format";
    type Telegram = {
      token: string
    }
    type CTelegramChat = {
      chat_id: string
    }
    export async function main(
      message: string, 
      name: string, 
      step_id: string, 
      auth: Telegram,
      telegramChat: CTelegramChat,
    ) {
      const bot = new Telegraf(auth.token)
    
      const baseURL = process.env.WM_BASE_URL
      const workspace = process.env.WM_WORKSPACE
    
      const rootFlowId = process.env.WM_ROOT_FLOW_JOB_ID
      const flowPath = process.env.WM_FLOW_PATH
      const rootFlowURL = `${baseURL}/run/${rootFlowId}?workspace=${workspace}`
    
      const schedulePath = process.env.WM_SCHEDULE_PATH
    
    
      let text = "";
      if (rootFlowId){
        const flow = await wmill.FlowService
          .getFlowByPath({workspace, path: flowPath})
          .catch(() => null)
        const flowModule = flow?.value?.modules?.find(m => m.id === step_id)
    
        text += `Step <b>[${step_id}]`
    
        if (flowModule){
          text += ` ${flowModule.summary}`;
        } 
        text += `</b>`
    
        if (flowModule?.value?.path){
          const scriptPath = flowModule?.value?.path;
          const script = await wmill.ScriptService
            .getScriptByPath({path: scriptPath, workspace})
            .catch(() => null);
    
          if (script){
            const scriptURL = `https://windmill.kdtsk.com/scripts/get/${script.hash}?workspace=${workspace}`
            text += ` (<a href="${scriptURL}">${scriptPath}</a>)`;
          } else {
            text += ` (${scriptPath})`;
          }
        }
    
        text += ` in\nFlow <a href="${rootFlowURL}">${flowPath}</a> `;
      } 
    
      if (schedulePath) {
        text += `triggered by ${schedulePath} schedule `
      }
    
      text += `\nhad an error:\n<pre><code class="language-${name}">${message}</code></pre>`;
    
      const res = await bot.telegram.sendMessage(telegramChat.chat_id, new FmtString(text), {
        parse_mode: "HTML",
      })
      
      return {res};
    }
    
    

    Submitted by nick kadutskyi374 415 days ago