Skip to content

Instantly share code, notes, and snippets.

@naosim
Created July 23, 2024 20:26
Show Gist options
  • Save naosim/5ae02463e1b5848a199b069e0f122cad to your computer and use it in GitHub Desktop.
Save naosim/5ae02463e1b5848a199b069e0f122cad to your computer and use it in GitHub Desktop.
slackに画像を送る
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