diff --git a/examples/edge-functions/supabase/.env.local.example b/examples/edge-functions/supabase/.env.local.example index d99d5930180..b69d3f546a5 100644 --- a/examples/edge-functions/supabase/.env.local.example +++ b/examples/edge-functions/supabase/.env.local.example @@ -1,5 +1,10 @@ +# send-email-smtp SMTP_HOSTNAME="your.hostname.com" SMTP_PORT="2587" SMTP_USERNAME="your_username" SMTP_PASSWORD="your_password" -SMTP_FROM="no-reply@example.com" \ No newline at end of file +SMTP_FROM="no-reply@example.com" + +# telegram-bot +TELEGRAM_BOT_TOKEN="get it from https://t.me/BotFather" +FUNCTION_SECRET="random secret" \ No newline at end of file diff --git a/examples/edge-functions/supabase/functions/send-email-smtp/index.ts b/examples/edge-functions/supabase/functions/send-email-smtp/index.ts index 5fa1615629c..4958b8fa153 100644 --- a/examples/edge-functions/supabase/functions/send-email-smtp/index.ts +++ b/examples/edge-functions/supabase/functions/send-email-smtp/index.ts @@ -7,6 +7,8 @@ import { SmtpClient } from 'https://deno.land/x/denomailer@0.12.0/mod.ts' const smtp = new SmtpClient() +console.log(`Function "send-email-smtp" up and running!`) + serve(async (req) => { await smtp.connect({ hostname: Deno.env.get('SMTP_HOSTNAME')!, diff --git a/examples/edge-functions/supabase/functions/telegram-bot/README.md b/examples/edge-functions/supabase/functions/telegram-bot/README.md new file mode 100644 index 00000000000..897e9a86dbb --- /dev/null +++ b/examples/edge-functions/supabase/functions/telegram-bot/README.md @@ -0,0 +1,13 @@ +# telegram-bot + +Try it out: https://t.me/supabase_example_bot + +![demo](https://obuldanrptloktxcffvn.supabase.co/storage/v1/object/public/images/supabase_community/telegram_bot/supa_telegram_bot.gif?t=2022-05-20T05:27:04.904Z) + +## Deploy + +1. Run `supabase functions deploy --no-verify-jwt telegram-bot` +2. Get your Telegram token from https://t.me/BotFather +3. Run `supabase secrets set TELEGRAM_BOT_TOKEN=your_token FUNCTION_SECRET=random_secret` +4. Set your bot's webhook url to `https://.functions.supabase.co/telegram-bot` (Replacing `<...>` with respective values). In order to do that, run this url (in your browser, for example): `https://api.telegram.org/bot/setWebhook?url=https://.functions.supabase.co/telegram-bot?secret=` +5. That's it, go ahead and chat with your bot 🤖💬 \ No newline at end of file diff --git a/examples/edge-functions/supabase/functions/telegram-bot/index.ts b/examples/edge-functions/supabase/functions/telegram-bot/index.ts new file mode 100644 index 00000000000..eab5aed9fd6 --- /dev/null +++ b/examples/edge-functions/supabase/functions/telegram-bot/index.ts @@ -0,0 +1,35 @@ +// Follow this setup guide to integrate the Deno language server with your editor: +// https://deno.land/manual/getting_started/setup_your_environment +// This enables autocomplete, go to definition, etc. + +import { serve } from "https://deno.land/std@0.131.0/http/server.ts" + +console.log(`Function "telegram-bot" up and running!`) + +import { Bot, webhookCallback } from "https://deno.land/x/grammy@v1.8.3/mod.ts"; + +const bot = new Bot(Deno.env.get('TELEGRAM_BOT_TOKEN') || ''); + +bot.command("start", (ctx) => ctx.reply("Welcome! Up and running.")); + +bot.command('ping', (ctx) => ctx.reply(`Pong! ${new Date()} ${Date.now()}`)) + +const handleUpdate = webhookCallback(bot, "std/http"); + +serve(async (req) => { + try { + const url = new URL(req.url); + if (url.searchParams.get('secret') !== Deno.env.get('FUNCTION_SECRET')) + return new Response('not allowed', { status: 405 }) + + return await handleUpdate(req); + } catch (err) { + console.error(err); + } +}) + +// To invoke: +// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \ +// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \ +// --header 'Content-Type: application/json' \ +// --data '{"name":"Functions"}' diff --git a/examples/edge-functions/supabase/functions/test/index.ts b/examples/edge-functions/supabase/functions/test/index.ts new file mode 100644 index 00000000000..820653ee25c --- /dev/null +++ b/examples/edge-functions/supabase/functions/test/index.ts @@ -0,0 +1,25 @@ +// Follow this setup guide to integrate the Deno language server with your editor: +// https://deno.land/manual/getting_started/setup_your_environment +// This enables autocomplete, go to definition, etc. + +import { serve } from "https://deno.land/std@0.131.0/http/server.ts" + +console.log("Hello from Functions!") + +serve(async (req) => { + const { name } = await req.json() + const data = { + message: `Hello ${name}!`, + } + + return new Response( + JSON.stringify(data), + { headers: { "Content-Type": "application/json" } }, + ) +}) + +// To invoke: +// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \ +// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \ +// --header 'Content-Type: application/json' \ +// --data '{"name":"Functions"}' diff --git a/web/docs/guides/functions.mdx b/web/docs/guides/functions.mdx index 94f89a3fd50..f0d803f6e4a 100644 --- a/web/docs/guides/functions.mdx +++ b/web/docs/guides/functions.mdx @@ -91,6 +91,14 @@ supabase functions deploy hello-world This command bundles your Edge Function from `./functions/hello-world/index.ts` and deploys it to the Supabase platform. The command outputs a URL to the Supabase Dashboard which you can open to find view more details. Let's open the link to find the execution command. +:::note + +By default Edge Functions require a valid JWT to be send in the authorization header. This header is automatically set when invoking your function via a Supabase client library. + +If you want to use Edge Functions to handle webhooks (e.g. [Stripe payment webhooks](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/stripe-webhooks), or [chat bot webhooks](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/telegram-bot) etc.), you need to pass the `--no-verify-jwt` flag when deploying your function. + +::: + ### Executing Remote Functions You can execute Edge Functions using curl. Copy the curl command from the Dashboard. It should look like this: