如何使用Google Apps脚本和Discord Webhook将图像上传到Discord? [英] How to upload an image to Discord using Google Apps Script and a Discord Webhook?

查看:226
本文介绍了如何使用Google Apps脚本和Discord Webhook将图像上传到Discord?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下脚本:

function uploadImageToDiscord() {

  var link = "https://i.imgur.com/image.jpg";
  var img = UrlFetchApp.fetch(link).getBlob();

  var discordUrl = "https://discordapp.com/api/webhooks/mywebhook";

  var payload = {
    "file": img
  };

  var params = {
    headers: {
        "Content-Type": "multipart/form-data"
    },
    method: "post",
    payload: payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(discordUrl, params);

  Logger.log(response.getContentText());

}

不过,GAS告诉我我正在尝试发送空消息。有人可以帮我吗?

However, GAS tells me that I'm trying to send an empty message. Can anyone help me?

错误消息

错误一定与我尝试下载图像的方式有关:

The error must be related to either the way of me trying to download the image:

var img = UrlFetchApp.fetch(link).getBlob();

或定义多部分/表单数据内容有效负载的方式:

or the way of how I define the payload for the multipart/form-data content:

 var payload = {
    "file": img
 };


推荐答案

此修改如何?

var params = {
  headers: {
      "Content-Type": "multipart/form-data"
  },
  method: "post",
  payload: payload,
  muteHttpExceptions: true
};



收件人:

To:

var params = {
  method: "post",
  payload: payload,
  muteHttpExceptions: true
};



其他信息:



例如,如果要将文本添加到文件中,请使用以下请求正文。

Additional information:

For example, if you want to add the text to the file, please use the following request body.

var payload = {
  content: "sample text", // Added
  file: img
};
var params = {
  method: "post",
  payload: payload,
  muteHttpExceptions: true
};



参考:




  • Webhook资源

  • Reference:

    • Webhook Resource
    • 在我的环境中,我正在使用这样的请求正文。而且效果很好。但是,如果在您的环境中无法正常工作,请告诉我。我想考虑其他解决方案。

      In my environment, I am using such request body. And it works fine. But if in your environment, it didn't work, please tell me. I would like to think of other solutions.

      这篇关于如何使用Google Apps脚本和Discord Webhook将图像上传到Discord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆