英文:
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, "Content-Type: application/json");
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, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
I call this function here
std::string message = "🔗Link: "+url+"\n🔑Password: |"+ zipPwd;
func::sendDiscordWebhook(config::Bot_url, message);
When I compiled and ran this code all worked well, I didn'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'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't just send a String.
</details>
# 答案2
**得分**: 0
以下是您要翻译的内容:
"通过Discord webhook发送的消息应完全符合JSON格式。您应该使用`\\n`代替`\n`,而且如果您想要正确的输出,您的消息应该具有如下格式:`std::string data`,而不是一行。"
```cpp
std::string title = "!新请求!";
std::string content = "\\n👤用户: " + func::GetUser() + "\\n🔗链接: " + url + "\\n🔑密码: " + 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 = "!New request!";
std::string content = "\\n👤User: " + func::GetUser() + "\\n🔗Link: " + url + "\\n🔑Password: " + zipPwd;
//std::cout << url;
std::string data = R"({
"content": null,
"embeds": [{
"title": ")" + title + R"(",
"description": ")" + content + R"(",
"color": 10181046
}]
})";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论