Created
July 23, 2024 20:26
-
-
Save naosim/5ae02463e1b5848a199b069e0f122cad to your computer and use it in GitHub Desktop.
slackに画像を送る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IncomingWebhookClient } from 'https://unpkg.com/@slack/webhook@latest/dist/cjs/index.js'; | |
import fs from 'fs'; | |
const url = 'YOUR_SLACK_WEBHOOK_URL'; // Incoming Webhook URL | |
const channel = '#general'; // 送信先チャンネル | |
const message = '画像を送信します'; // メッセージ | |
const filePath = 'image.jpg'; // 送信する画像ファイルのパス | |
(async () => { | |
try { | |
const file = await fs.promises.readFile(filePath); | |
const formData = new FormData(); | |
formData.append('channel', channel); | |
formData.append('message', message); | |
formData.append('file', file, 'image.jpg'); | |
const response = await client.post(formData); | |
console.log('Success:', response.data); | |
} catch (error) { | |
console.error('Error:', error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment