wechat-bot.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * 企业微信机器人推送(Markdown 文字 + 图片)
  3. * 运行环境:青龙 Node.js
  4. */
  5. const axios = require('axios');
  6. const crypto = require('crypto');
  7. // ======= 配置区 =======
  8. const WEBHOOK_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=795d0b1e-20e9-40ab-979c-a5752cd2de67'; // 机器人Webhook
  9. const TEXT_CONTENT = '## 温馨提示:\n\n 预算创建时,🔴 **预算模板**需勾选,⚙️ **取费标准**需勾选。📝 请确保所有选项正确设置。\n\n请参考下方图片 👇 可点击放大';
  10. const IMAGE_URL = 'https://p.sda1.dev/27/ddc36bcbf0ebe6c136ca363be580ac93/PixPin_2025-09-16_12-04-46.png'; // 图片URL
  11. // =====================
  12. // 发送Markdown格式的消息
  13. async function sendMarkdown(text) {
  14. try {
  15. const payload = {
  16. msgtype: 'markdown',
  17. markdown: {
  18. content: text,
  19. }
  20. };
  21. const res = await axios.post(WEBHOOK_URL, payload);
  22. console.log('Markdown文字推送结果:', res.data);
  23. } catch (err) {
  24. console.error('Markdown文字推送失败:', err);
  25. }
  26. }
  27. // 发送图片消息(支持网络URL)
  28. async function sendImage(imageUrl) {
  29. try {
  30. const res = await axios.get(imageUrl, { responseType: 'arraybuffer' });
  31. const imgData = Buffer.from(res.data);
  32. const base64Img = imgData.toString('base64');
  33. const md5 = crypto.createHash('md5').update(imgData).digest('hex');
  34. const payload = {
  35. msgtype: 'image',
  36. image: {
  37. base64: base64Img,
  38. md5: md5
  39. }
  40. };
  41. const response = await axios.post(WEBHOOK_URL, payload);
  42. console.log('图片推送结果:', response.data);
  43. } catch (err) {
  44. console.error('图片推送失败:', err);
  45. }
  46. }
  47. // 主函数
  48. (async () => {
  49. await sendMarkdown(TEXT_CONTENT); // 发送Markdown格式的文字
  50. await sendImage(IMAGE_URL); // 发送图片
  51. })();