Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update google-cloud-function.ts #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions examples/functions/google-cloud-function.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
// TODO: this needs its own full example like the one for aws-lambda
// here's a real working example of using firebase functions via a webhook

import { Telegraf } from "telegraf";
import { onRequest } from 'firebase-functions/v2/https';
import * as logger from 'firebase-functions/logger';
import { Telegraf, Context } from 'telegraf';
import { message } from 'telegraf/filters';

const bot = new Telegraf(token);
// create a new bot instance that will handle the updates and reply via webhook
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN as string,{telegram: { webhookReply: true }});

bot.command("hello", ctx => ctx.reply("Hello, friend!"));
// register the commands and event handlers IN THE ORDER they will be executed
bot.command('start', (ctx: Context) => ctx.reply('Welcome!'));
bot.command('help', (ctx: Context) => ctx.reply('How can I assist you?'));
bot.hears('hi', (ctx) => ctx.reply('Hey there'));
bot.on(message('sticker'), (ctx) => ctx.reply('👍'));
bot.on(message('new_chat_members'), async (ctx) => {
logger.info('new_chat_members:', ctx.update.message.new_chat_members);
const newMembers = ctx.message.new_chat_members;
newMembers.forEach((member) => {
ctx.reply(`Welcome ${member.first_name} to the group!`);
});
});

// message seems to be the default handler for most other message type
bot.on(message('text'), (ctx: Context) => ctx.reply('Hello!'));

export const myBot = onRequest(async (request, response) => {
try {
logger.log(JSON.stringify(request.body)); // for debugging - remove in production
await bot.handleUpdate(request.body, response);
response.status(200).end();
} catch (err) {
logger.error('Failed to process update', err);
response.sendStatus(500);
}
});

export const botFunction = bot.webhookCallback(bot.secretPathComponent());