Repository: defstudio/telegraph (php)

Issue:
Custom WebHook URL
I use a local Telegram Bot server. When registering, the WebHook is installed based on the APP_URL configuration variable. But my URL inside the server starts with http://127.0.0.1:8080 (for example).

As an option, make a separate option in the configuration.

Pull request description:
[feat] custom webhook domain
Adds a new `custom_webhook_domain` configuration key to force a different domain when registering a webhook

fix #254 

Patch:
diff --git a/config/telegraph.php b/config/telegraph.php
index ef35be77..cce5a7ed 100644
--- a/config/telegraph.php
+++ b/config/telegraph.php
@@ -23,6 +23,14 @@ return [
      */
     'webhook_handler' => DefStudio\Telegraph\Handlers\EmptyWebhookHandler::class,
 
+    /*
+     * Sets a custom domain when registering a webhook. This will allow a loca telegram bot api server
+     * to reach the webhook. Disabled by default
+     *
+     * For reference, see https://core.telegram.org/bots/api#using-a-local-bot-api-server
+     */
+    // 'custom_webhook_domain' => 'http://my.custom.domain';
+
     /*
      * If enabled, Telegraph dumps received
      * webhook messages to logs
diff --git a/docs/content/en/installation.md b/docs/content/en/installation.md
index 3d7f2875..46df3c0f 100644
--- a/docs/content/en/installation.md
+++ b/docs/content/en/installation.md
@@ -54,6 +54,14 @@ return [
      * For reference, see https://defstudio.github.io/telegraph/webhooks/overview
      */
     'webhook_handler' => EmptyWebhookHandler::class,
+    
+    /*
+     * Sets a custom domain when registering a webhook. This will allow a loca telegram bot api server
+     * to reach the webhook. Disabled by default
+     *
+     * For reference, see https://core.telegram.org/bots/api#using-a-local-bot-api-server
+     */
+    // 'custom_webhook_domain' => 'http://my.custom.domain';
 
     /*
      * If enabled, Telegraph dumps received
diff --git a/src/Concerns/InteractsWithWebhooks.php b/src/Concerns/InteractsWithWebhooks.php
index 5bf79995..e3455c24 100644
--- a/src/Concerns/InteractsWithWebhooks.php
+++ b/src/Concerns/InteractsWithWebhooks.php
@@ -12,19 +12,30 @@ use DefStudio\Telegraph\Telegraph;
  */
 trait InteractsWithWebhooks
 {
-    public function registerWebhook(): Telegraph
+    private function getWebhookUrl(): string
     {
-        $telegraph = clone $this;
+        $customWebhookUrl = config('custom_webhook_domain');
+
+        if ($customWebhookUrl === null) {
+            $url = route('telegraph.webhook', $this->getBot());
 
-        $url = route('telegraph.webhook', $telegraph->getBot());
+            if (!str_starts_with($url, 'https://')) {
+                throw TelegramWebhookException::invalidScheme();
+            }
 
-        if (!str_starts_with($url, 'https://')) {
-            throw TelegramWebhookException::invalidScheme();
+            return $url;
         }
 
+        return $customWebhookUrl . route('telegraph.webhook', $this->getBot(), false);
+    }
+
+    public function registerWebhook(): Telegraph
+    {
+        $telegraph = clone $this;
+
         $telegraph->endpoint = self::ENDPOINT_SET_WEBHOOK;
         $telegraph->data = [
-            'url' => $url,
+            'url' => $this->getWebhookUrl(),
         ];
 
         return $telegraph;


Test patch:
diff --git a/tests/Unit/Concerns/InteractsWithWebhooksTest.php b/tests/Unit/Concerns/InteractsWithWebhooksTest.php
index 277b9344..a4a56d09 100644
--- a/tests/Unit/Concerns/InteractsWithWebhooksTest.php
+++ b/tests/Unit/Concerns/InteractsWithWebhooksTest.php
@@ -11,6 +11,15 @@ it('can register a webhook', function () {
         ->toMatchTelegramSnapshot();
 });
 
+it('can register a webhook with a custom domain', function () {
+    withfakeUrl();
+
+    config()->set('custom_webhook_domain', 'http://foo.bar.baz');
+
+    expect(fn (Telegraph $telegraph) => $telegraph->bot(make_bot())->registerWebhook())
+        ->toMatchTelegramSnapshot();
+});
+
 it('requires an https url to register a webhook', function () {
     \DefStudio\Telegraph\Facades\Telegraph::bot(make_bot())->registerWebhook();
 })->throws(TelegramWebhookException::class, 'You application must have a secure (https) url in order to accept webhook calls');
diff --git a/tests/__snapshots__/InteractsWithWebhooksTest__it_can_register_a_webhook_with_a_custom_domain__1.yml b/tests/__snapshots__/InteractsWithWebhooksTest__it_can_register_a_webhook_with_a_custom_domain__1.yml
new file mode 100644
index 00000000..f5cf623d
--- /dev/null
+++ b/tests/__snapshots__/InteractsWithWebhooksTest__it_can_register_a_webhook_with_a_custom_domain__1.yml
@@ -0,0 +1,4 @@
+url: 'https://api.telegram.org/bot3f3814e1-5836-3d77-904e-60f64b15df36/setWebhook'
+payload:
+    url: 'http://foo.bar.baz/telegraph/3f3814e1-5836-3d77-904e-60f64b15df36/webhook'
+files: {  }