Repository navigation
Web app Telegram.WebApp.sendData event on bot's side #1206
Replies: 2 comments 1 reply
const TelegramBot = require('node-telegram-bot-api');
const TOKEN = 'YOUR_BOT_TOKEN';
const bot = new TelegramBot(TOKEN, { polling: true });
// Handle /start command to send an initial message
bot.on('message', (msg) => {
if (msg.text.toString() === '/start') {
bot.sendMessage(msg.chat.id, 'Hello! Click the button below to send data from the Web App.', {
reply_markup: {
inline_keyboard: [
[{ text: 'Send Data', callback_data: 'get_web_app_data' }],
],
},
});
}
});
// Handle the callback query from the button
bot.on('callback_query', (query) => {
if (query.data === 'get_web_app_data') {
const webAppData = query.message.web_app_data; // Access data here!
bot.sendMessage(query.message.chat.id, `Web App Data: ${webAppData?.data}`);
}
}); |
|
ngl ini jebakan klasik webapp tbh. senddata itu cuma jalan kalau webapp dibuka dari tombol keyboard biasa, bukan dari inline keyboard. kalau dibuka dari inline keyboard, datanya ga akan nyampe ke bot sama sekali, makanya di sisi bot ga ada event apa apa. alurnya tuh gini. kirim tombol keyboard yang isinya webapp ke user. user ketuk tombol itu, webapp kebuka. di dalam webapp panggil telegram.webapp.senddata. telegram langsung kirim message ke bot dan datanya nempel di msg.webappdata.data. jadi di sisi bot jangan dengerin callbackquery, dengerin message aja. contohnya gini. bot.on('message', (msg) => {
if (msg.webappdata) {
console.log(msg.webappdata.data)
}
})tombolnya sendiri mesti dibikin kayak gini, perhatiin bagian webapp di dalam keyboardnya. bot.sendmessage(chatid, 'buka webapp', {
replymarkup: {
keyboard: [[{ text: 'buka', webapp: { url: 'https://contoh.com/app' } }]]
}
})btw satu hal lagi, senddata ini nutup webapp otomatis setelah data kekirim. kalau mau webapp tetap kebuka sambil kirim data berkali kali, itu ga bisa pakai senddata, mesti pakai cara lain kayak kirim data lewat bot api dari server sendiri. |
Uh oh!
There was an error while loading. Please reload this page.
I am trying to send some data in my web app to my bot:
Telegram.WebApp.sendData("test_data");But I don't see any event on bot's side, like "message" or "callback_query" with my data inside.
Am I missing something?
All reactions