feat: add telegram-bot edge functions example

This commit is contained in:
thorwebdev committed 2022-05-20 13:31:33 +08:00
1 parent 035e6c13c5
commit 588ea0404a
6 files changed
+89 -1

No files matched your search

@@ -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"
SMTP_FROM="no-reply@example.com"
# telegram-bot
TELEGRAM_BOT_TOKEN="get it from https://t.me/BotFather"
FUNCTION_SECRET="random secret"
@@ -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')!,
@@ -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://<PROJECT_NAME>.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<TELEGRAM_BOT_TOKEN>/setWebhook?url=https://<PROJECT_NAME>.functions.supabase.co/telegram-bot?secret=<FUNCTION_SECRET>`
5. That's it, go ahead and chat with your bot 🤖💬
@@ -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"}'
@@ -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"}'
+8
View File
@@ -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: