> ## Documentation Index
> Fetch the complete documentation index at: https://gowa-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# メッセージの受信

> 着信メッセージをWebフックで処理したり、GOWAのチャットストレージから保存されたチャットやメッセージを読んだりできます。

メッセージの受信方法は以下の2つです。

| 方法 | それを使用する |
| - | - |
| **Webhooks** (推奨) | リアルタイムでイベントに反応します。GOWAは各イベントをURLにプッシュします。 |
| **チャットストレージ API** | 読書履歴, ダウンタイムの後に追いつく, 受信トレイビューを構築. |

## Webhooks

GOWAをWebhookのURLとシークレットで開始します。

```bash theme={null}
./whatsapp rest --basic-auth=admin:changeme \
  --webhook=https://example.com/wa-hook \
  --webhook-secret=change-me \
  --webhook-events=message,message.ack
```

次のようにテキストメッセージが到着します:

```json theme={null}
{
  "event": "message",
  "device_id": "628123456789@s.whatsapp.net",
  "session_id": "my-device",
  "payload": {
    "id": "3EB0C127D7BACC83D6A1",
    "chat_id": "628987654321@s.whatsapp.net",
    "from": "628987654321@s.whatsapp.net",
    "from_name": "John Doe",
    "sender_display_name": "John Doe",
    "timestamp": "2026-10-15T10:30:00Z",
    "is_from_me": false,
    "body": "Hello, how are you?"
  }
}
```

グループ内では、`chat_id` はグループJID (`...@g.us`)で、`from` はそれを送信したメンバーです。

### 例: 自動返信ボット

署名を確認し、確認し、同じデバイスで API 経由で返信します。

```javascript theme={null}
const express = require("express");
const crypto = require("crypto");

const GOWA = "http://localhost:3000";
const AUTH = "Basic " + Buffer.from("admin:changeme").toString("base64");
const SECRET = "change-me";

const app = express();
app.post("/wa-hook", express.raw({ type: "application/json" }), async (req, res) => {
  const sig = crypto.createHmac("sha256", SECRET).update(req.body).digest("hex");
  const received = req.get("X-Hub-Signature-256") || "";
  const expected = `sha256=${sig}`;
  if (!/^sha256=[0-9a-f]{64}$/.test(received) ||
      received.length !== expected.length ||
      !crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected))) {
    return res.sendStatus(401);
  }
  const evt = JSON.parse(req.body);
  res.sendStatus(200);
  if (evt.event !== "message" || evt.payload.is_from_me) return;
  if (!evt.session_id) return;
  if (!/^hi\b/i.test(evt.payload.body || "")) return;

  const response = await fetch(`${GOWA}/send/message`, {
    method: "POST",
    headers: {
      Authorization: AUTH,
      "Content-Type": "application/json",
      "X-Device-Id": evt.session_id,          // reply from the slot that received it
    },
    body: JSON.stringify({
      phone: evt.payload.chat_id,
      message: "Hello! How can we help?",
      reply_message_id: evt.payload.id,
    }),
  });
  if (!response.ok) console.error("GOWA reply failed", response.status);
});
app.listen(8080);
```

この最小限の例では、返信を送信する前に確認します。 本番環境では、確認されたイベントを継続的にキューに入れてから、デバイス、チャット、およびメッセージ ID で重複排除し、ワーカーでの送信に失敗しました。 `session_id` なしでイベントをスキップしたり、正しいデバイスを明示的に解決したりします。

すべてのメッセージへの固定返信のために、 `WHATSAPP_AUTO_REPLY="We'll get back to you soon"` はコードなしでこれを行います。

すべてのイベント タイプ、メディア フィールド、再試行、デバイス URL については、 [Webhooks](https://gowa-docs.mintlify.site/ja/concepts/webhooks.md) を参照してください。

### 新しいイベントシェイプを処理する

アルバムヘッダは `payload.album` を含む `message` イベントで、テキスト本文はありません。それぞれの画像やビデオは `payload.album_id`。ヘッダーを欠落しているメッセージとして扱う代わりに、そのIDでメディアをグループ化します。

`newsletter.message` にはチャンネルカウンターの更新が含まれており、新しい投稿の本文を保証するものではありません。 オンラインおよびオフラインの連絡先の更新は `presence` を使用し、`POST /user/presence/subscribe` を必要とし、クライアントが利用可能なままにする必要があります。グループ名の変更は `group.info` を使用します。

`device.logged_out`、`device.temporary_ban`、および`device.reachout_timelock`を購読して、アカウントの制限と通常の切断を区別します。 これらのイベントは常に `session_id` でスロットを識別します。フィールドとプレゼンス設定については [Webhooks](https://gowa-docs.mintlify.site/ja/concepts/webhooks.md) を参照してください。

## チャットストレージAPI

GOWAは、ローカルチャットストレージ内の各スロットのチャットやメッセージを記録します。デバイスごとに問い合わせる:

```bash theme={null}
# list chats (filters: search, has_media, archived; paging: limit, offset)
curl -u admin:changeme -H "X-Device-Id: my-device" \
  "http://localhost:3000/chats?limit=20"

# messages in one chat (filters: start_time, end_time, media_only, is_from_me, search)
curl -u admin:changeme -H "X-Device-Id: my-device" \
  "http://localhost:3000/chat/628987654321@s.whatsapp.net/messages?limit=50"
```

GOWAが閲覧したメッセージ(接続中、またはWhatsAppの履歴同期によって配信中)のみが利用可能です。 チャットで古い履歴を携帯電話に問い合わせるには、`{"count": 50}`で`POST /chat/{chat_jid}/history`を呼び出します。メッセージは非同期に到着します。

ポーリングはキャッチアップとインボックスビューに適しています。ボットやオートメーションの場合は、Webhookを使用してください。

## メディアをダウンロード中

Webhookは`statics/media/`の下にパスを与え、`http://localhost:3000/statics/media/<file>`で提供されます。そうでなければ、要求に応じてメディアをフェッチします。

```bash theme={null}
curl -u admin:changeme -H "X-Device-Id: my-device" \
  "http://localhost:3000/message/3EB0C127D7BACC83D6A3/download?phone=628987654321@s.whatsapp.net"
```

GOWAが期限切れファイルを復元できる条件については、[Incoming media](https://gowa-docs.mintlify.site/ja/examples/media-handling.md#%E3%83%A1%E3%83%87%E3%82%A3%E3%82%A2%E3%81%AE%E7%9D%80%E4%BF%A1)を参照してください。

## 開封通知

`POST /message/{message_id}/read` と `{"phone": "<chat_jid>"}`, または、 `WHATSAPP_AUTO_MARK_READ=true` を設定すると、すべての受信メッセージが自動的に読み込まれます。


## Related topics

- [メッセージを送信中](https://gowa-docs.mintlify.site/ja/examples/sending-messages.md)
- [メッセージを送信](https://gowa-docs.mintlify.site/ja/api-reference/send/メッセージを送信.md)
- [実験的なインタラクティブメッセージを送信する](https://gowa-docs.mintlify.site/ja/api-reference/send/実験的なインタラクティブメッセージを送信する.md)
- [実験的な対話メッセージ](https://gowa-docs.mintlify.site/ja/examples/interactive-messages.md)
- [ニュースレターメッセージカウンターを取得する](https://gowa-docs.mintlify.site/ja/api-reference/newsletter/ニュースレターメッセージカウンターを取得する.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.