LoginSignup
1
1

More than 3 years have passed since last update.

Siv3DでDiscord Webhookのクライアントを作ってみた話

Last updated at Posted at 2019-12-15

作ったもの

ソースコードはここです:https://github.com/hota1024/discord-webhook-test-with-opensiv3d

Discord Webhookとは

ゲーマー向けのチャットサービスであるDiscordにはWebhookという機能があります。
Webhookを使うとボットを作らなくてもWebhookのURLにPOSTリクエストを投げることでDiscordのチャットにメッセージを送ることができます。

また例として、GithubとDiscord Webookを連携させることもできます。

git push origin masterした時のスクショ。
参考:https://support.discordapp.com/hc/ja/articles/228383668-タイトル-Webhooksへの序章
スクリーンショット 2019-12-14 20.10.38.png

C++でHTTP

今回C++でHTTP通信を行うためにcurllibを使用しました。

main.cpp
// POSTリクエストを送る例
# include <curl/curl.h>

int main()
{
  // curl の初期化
  CURL *curl;
  curl = curl_easy_init();

  // curl の設定
  curl_easy_setopt(curl, CURLOPT_POST, 1); // メソッドをPOSTに設定
  curl_easy_setopt(curl, CURLOPT_URL, "<URL>"); // URLを設定
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "<POST_DATA>"); // データを設定
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L); // データのサイズを指定

  // 送信
  curl_easy_perform(curl);

  // クリーンアップ
  curl_easy_cleanup(curl);

  return 0;
}

今回の詰まった&詰まっているポイント

今回作ったソースコードだと何故かjsonでデータを送ることができませんでした...
下のようなレスポンスが返ってくる...

{
  "message": "Cannot send an empty message",
  "code": 50006
}

感想

もうちょっと頑張ってDiscordのGateway(ボット)のプログラムをC++で書いてみたいなと思いました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1