I want to store and receive data from firebase realtime database by Telegram bot using Telegraf.
I'm using Firebase Functions / Cloud Functions for this.
Right now storing data on database is working, but I can't figure out how to get data from firebase and then send it as telegram message.
Below is response for /database command
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Telegraf = require('telegraf');
admin.initializeApp();
const bot = new Telegraf(functions.config().telegram_token.key);
bot.hears('hello', (ctx) => ctx.reply('Hi from Firebase Function!'));
bot.command('database', (ctx) => {
// store data on firebase realtime database - it's working
admin.database().ref('users/' + ctx.message.chat.id).set({
username: ctx.message.from.first_name,
email: 'test@test.gmail.com',
});
// HOW TO? get data from firebase realtime database
const userId = ctx.message.chat.id;
const dataFromDb = admin.database().ref('users/' + userId).once('value');
ctx.reply(`Data from firebase: ${dataFromDb}`);
});
bot.launch();
exports.bot = functions.region('europe-west1').https.onRequest((req, res) => {
bot.handleUpdate(req.body, res).then( (rv) => !rv && res.sendStatus(200));
});
Can someone please share useful example? :)
Thank U for your attencion,
Regards!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…