Error with sending message using discord webhook

huangapple go评论71阅读模式
英文:

Error with sending message using discord webhook

问题

我想在C++ libcurl中使用Discord webhook发送消息。所以我写了这个函数
```cpp
void func::sendDiscordWebhook(const std::string& webhookUrl, const std::string& content)
{
    CURL* curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, webhookUrl.c_str());

        struct curl_slist* list = NULL;
        list = curl_slist_append(list, "Content-Type: application/json");

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());

        res = curl_easy_perform(curl);
        curl_slist_free_all(list);

        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
}

我在这里调用这个函数

std::string message = "📷链接:" + url + "\n🔓密码:|" + zipPwd;

func::sendDiscordWebhook(config::Bot_url, message);

当我编译和运行这段代码时,一切都正常,没有出现任何错误,但当我检查Discord时,没有消息。


<details>
<summary>英文:</summary>

I want to send a message using Discord webhook in c++ libcurl. So I wrote this function

void func::sendDiscordWebhook(const std::string& webhookUrl, const std::string& content)
{
CURL* curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_ALL);

curl = curl_easy_init();
if (curl) {
	curl_easy_setopt(curl, CURLOPT_URL, webhookUrl.c_str());

	struct curl_slist* list = NULL;
	list = curl_slist_append(list, &quot;Content-Type: application/json&quot;);

	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);

	res = curl_easy_perform(curl);
	curl_slist_free_all(list);     

	if (res != CURLE_OK)
		fprintf(stderr, &quot;curl_easy_perform() failed: %s\n&quot;,
			curl_easy_strerror(res));

	curl_easy_cleanup(curl);
}
curl_global_cleanup();

}

I call this function here
std::string message = &quot;&#128279;Link: &quot;+url+&quot;\n&#128273;Password: |&quot;+ zipPwd;

func::sendDiscordWebhook(config::Bot_url, message);
When I compiled and ran this code all worked well, I didn&#39;t get any error, but when I check Discord there was no message.

</details>


# 答案1
**得分**: 0

[CURLOPT_POSTFIELDS](https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html) 需要一个 `char *`,也就是 `char[]` 或 C 风格的字符串。尝试用以下方式替换
`curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);`:
`curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());`。

此外,[Discord的Webhook文档](https://discord.com/developers/docs/resources/webhook#execute-webhook) 指出你需要将你的 postdata(代码中的 `content` 变量)格式化为 JSON,不能直接发送一个字符串。

<details>
<summary>英文:</summary>

[CURLOPT_POSTFIELDS](https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html) requires a `char *`, aka `char[]` or C-style string. Try replacing
`curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);` with  
`curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content.c_str());`.

Additionally, [Discord&#39;s webhook docs](https://discord.com/developers/docs/resources/webhook#execute-webhook) say that you need to format your postdata (the `content` variable in your code) as a JSON, and you can&#39;t just send a String.

</details>



# 答案2
**得分**: 0

以下是您要翻译的内容:

"通过Discord webhook发送的消息应完全符合JSON格式。您应该使用`\\n`代替`\n`,而且如果您想要正确的输出,您的消息应该具有如下格式:`std::string data`,而不是一行。"

```cpp
std::string title = "!新请求!";
std::string content = "\\n&#128100;用户: " + func::GetUser() + "\\n&#128279;链接: " + url + "\\n&#128273;密码: " + zipPwd;

//std::cout << url;

std::string data = R"({
    "content": null,
    "embeds": [{
        "title": ")" + title + R"(",
        "description": ")" + content + R"(",
        "color": 10181046
    }]
})";
英文:

A message which you want to send via Discord webhook should be fully json adapted. You should use \\n instead of \n, also if you want to get the correct output your massage should have format like std::string data not in one line.

std::string title = &quot;!New request!&quot;;
    std::string content = &quot;\\n&#128100;User: &quot; + func::GetUser() + &quot;\\n&#128279;Link: &quot; + url + &quot;\\n&#128273;Password: &quot; + zipPwd;

    //std::cout &lt;&lt; url;

    std::string data = R&quot;({
        &quot;content&quot;: null,
        &quot;embeds&quot;: [{
            &quot;title&quot;: &quot;)&quot; + title + R&quot;(&quot;,
            &quot;description&quot;: &quot;)&quot; + content + R&quot;(&quot;,
            &quot;color&quot;: 10181046
        }]
    })&quot;;

huangapple
  • 本文由 发表于 2023年6月26日 01:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定