Closed TelegramXPlus closed 1 year ago
Can you show me your code?
If your webapp launched via a Keyboard button
, you can use sendData
function to send data to the bot, in bot updates proc, check for existent of Message
's webAppData
in and process the data
The code for the HTML is that:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
</head>
<body>
<button onclick="handleClick">Click</button>
<script>
Telegram.WebApp.ready();
function handleClick() {
Telegram.WebApp.sendData("test");
}
</script>
</body>
</html>
I've used ngrok to host the site
Whereas the code for the bot:
import telebot, asyncdispatch, logging, options, strutils
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
addHandler(L)
const API_KEY = "YOUR_TOKEN"
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async.} =
echo "something"
let bot = newTeleBot(API_KEY)
bot.onUpdate(updateHandler)
bot.poll(timeout=300)
I've seen the examples, but I supposed I could send the data using directly Nim; the issue is that later I'd like to use Svelte to deal with the html
You should check for webAppData
in Message
received in updateHandler
The point is that it's not even printing out "something"
Did you open webapp inside Telegram?
Yep, and I've tested even across multiple languages (but they give the same problem). Ofc I do believe the issue is mine
Ok, I've tried sending data through the client by opening the website with a callback query, it works, but I'd like to use the default botfather /setmenubutton
there are 2 ways to send data to bot use sendData
or use webApp
in InlineKeyboardButton
https://core.telegram.org/bots/webapps#keyboard-button-web-apps
But do I have to use the WebApp object inside the telebot library? Because I'd have liked to just use the HTML and then parse data through my update handler
I hadn't done a webapp before, but there are to ways to get data from webapp:
sendData
then it is webAppData
in MessageAnd that works only if I used a keyboardbutton to show the web app, otherwise, it does not work. It's not a big deal because sendData sends me data back, but I would have liked to know wether there was a solution for that.
Apart from this, may I ask how I handle callback queries? I've seen that update as a parameter, but how do I say that the keyboard button has a callback query?
I have got an HTML where when a button is clicked, it sends data to the bot, it uses an updateHandler because I thought it could handle every update, but it is not going there