<?php

$events = $this->getParam('events', []);

?>
<p>Webhooks allow you to build or set up integrations which subscribe to certain events on Appwrite. When one of those events is triggered, we'll send an HTTP POST payload to the webhook's configured URL. Webhooks can be used to purge cache from CDN, calculate data or send a Slack notification. You're only limited by your imagination.</p>

<h2><a href="/docs/webhooks#addWebhook" id="addWebhook">Add Your Webhook</a></h2>

<p>You can set your webhook by adding it from your Appwrite project dashboard. You can access your webhooks settings from your project dashboard or on the left navigation panel. Click the 'Add Webhook' button and choose your webhook name and the events that should trigger it. You can also set an optional basic HTTP authentication username and password to protect your endpoint from unauthorized access. </p>

<h2><a href="/docs/webhooks#payload" id="payload">Payload</a></h2>

<p>Each event type has a specific payload format with the relevant event information. All event payloads mirror the payloads for the API payload which parallel to the <a href="/docs/events">event types</a>.</p>

<h2><a href="/docs/webhooks#headers" id="headers">Headers</a></h2>

<p>HTTP requests made to your webhook's configured URL endpoint will contain several special headers:</p>

<table cellspacing="0" cellpadding="0" border="0" class="full text-size-small">
    <thead>
    <tr>
        <th style="width: 220px">Header</th>
        <th>Description</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>
            X-Appwrite-Webhook-Id
            <p class="margin-top-small text-fade">version >= 0.7</p>
        </td>
        <td>The ID of the Webhook who triggered the event.</td>
    </tr>
    <tr>
        <td>X-Appwrite-Webhook-Events</td>
        <td>Names of the events that triggered this delivery.</td>
    </tr>
    <tr>
        <td>X-Appwrite-Webhook-Name</td>
        <td>Name of the webhook as specified in your app settings and <a href="/docs/events">events list</a>.</td>
    </tr>
    <tr>
        <td>
            X-Appwrite-Webhook-User-Id
            <p class="margin-top-small text-fade">version >= 0.7</p>
        </td>
        <td>The user ID of the user who triggered the event. Returns an empty string if an API key triggered the event.<br /><br />Note that events like `account.create` or `account.sessions.create` are performed by guest users and will not return any user ID. If you still need the user ID for these events, you can find it in the event payload.</td>
    </tr>
    <tr>
        <td>
            X-Appwrite-Webhook-Project-Id
            <p class="margin-top-small text-fade">version >= 0.7</p>
        </td>
        <td>The ID of the project who owns the Webhook and API call.</td>
    </tr>
    <tr>
        <td>
            X-Appwrite-Webhook-Signature
            <p class="margin-top-small text-fade">version >= 0.15.0</p>
        </td>
        <td>The HMAC-SHA1 signature of the payload. This is used to verify the authenticity of the payload.</td>
    </tr>
    <tr>
        <td>User-Agent</td>
        <td>Each request made by Appwrite will be 'Appwrite-Server'.</td>
    </tr>
    </tbody>
</table>

<h2 id="verification"><a href="/docs/webhooks#verification">Verification</a></h2>

<p>Webhooks can be verified by using the <a href="/docs/webhooks#headers">X-Appwrite-Webhook-Signature</a> header. This is the HMAC-SHA1 
signature of the payload. You can find the signature key in your webhooks properties in the dashboard. To generate this hash you append
the payload to the end of webhook URL (make sure there are no spaces in between) and then use the HMAC-SHA1 algorithm to generate the signature.

After you've generated the signature, compare it to the "X-Appwrite-Webhook-Signature" header value. If they match, the payload is valid and you can trust it came from
your Appwrite instance. </p>

<!-- CODE EXAMPLES FOR THIS SECTION, Uncomment and finish when we have time -->

<!-- 
An example of how to generate the signature is shown below:</p>

<ul class="phases clear" data-ui-phases>
    <li>
        <h3>NodeJS</h3>
        <div class="ide margin-bottom" data-lang="javascript" data-lang-label="NodeJS">
            <pre class="line-numbers"><code class="prism language-javascript" data-prism>const crypto = require('crypto');
let token = crypto.createHmac("sha1", process.env.WEBHOOK_SIG_KEY)
    .update(`https://yourwebhookurl/test${payload.body}`) // Make sure there isn't a space between the URL and body.
    .digest().toString('base64');

if (token !== payload.headers['x-appwrite-webhook-signature']) {
    throw new Error('Failed authentication check.')
}</code></pre>
        </div>
    </li>
    <li>
        <h3>PHP</h3>

        <div class="ide margin-bottom" data-lang="php" data-lang-label="PHP">
            <pre class="line-numbers"><code class="prism language-dart" data-prism>$token = base64_encode(hash_hmac('sha1', 'https://yourwebhookurl/test' . $payload.body, getenv('WEBHOOK_SIG_KEY'), true));

if ($token != $payload.headers['x-appwrite-webhook-signature']) {
    throw new Error('Failed authentication check.');
}</code></pre>
        </div>
    </li>
    <li>
        <h3>Android</h3>
        <div class="ide margin-bottom" data-lang="android" data-lang-label="Android SDK">
            <pre class="line-numbers"><code class="prism language-kotlin" data-prism>val client = Client(context)

client
    .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
    .setProject("[PROJECT_ID]") // Your project ID

val realtime = Realtime(client)

// Subscribe to files channel
realtime.subscribe("files", callback = { response ->
    if(response.events.contains("buckets.*.files.*.create")) {
        // Log when a new file is uploaded
        print(response.payload.toString());
    }
})</code></pre>
        </div>
    </li>
    <li>
        <h3>Apple</h3>
        <div class="ide margin-bottom" data-lang="apple" data-lang-label="Apple SDK">
            <pre class="line-numbers"><code class="prism language-swift" data-prism>let client = Client()

client
    .setEndpoint("https://cloud.appwrite.io/v1") // Your API Endpoint
    .setProject("[PROJECT_ID]") // Your project ID

let realtime = Realtime(client)

// Subscribe to files channel
let subscription = realtime.subscribe(channels: ["files"]) { message in
    if(message.events!.contains("buckets.*.files.*.create")) {
        // Log when a new file is uploaded
        print(String(describing: message.payload))
    }
}</code></pre>
        </div>
    </li>
</ul> -->

<h2 id="events"><a href="/docs/events">Events</a></h2>
<p>Appwrite has events that fire when a resource changes. These events cover all Appwrite resources and can reflect create, update, and delete actions. You can specify one or many events to subscribe to with webhooks.</p>
<ul>
    <li class="margin-bottom"><a href="/docs/keys" rel="noopener"><i class="icon-angle-circled-right margin-start-negative-tiny margin-end-tiny"></i> Learn more about events</a></li>
</ul>