ba0f3 / telebot.nim

Async Telegram Bot API Client implement in @Nim-Lang
MIT License
165 stars 24 forks source link

How to handle WebApp data sent by the client #78

Closed TelegramXPlus closed 1 year ago

TelegramXPlus commented 1 year ago

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

ba0f3 commented 1 year ago

Can you show me your code?

ba0f3 commented 1 year ago

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

TelegramXPlus commented 1 year ago

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

ba0f3 commented 1 year ago

You should check for webAppData in Message received in updateHandler

TelegramXPlus commented 1 year ago

The point is that it's not even printing out "something"

ba0f3 commented 1 year ago

Did you open webapp inside Telegram?

TelegramXPlus commented 1 year ago

Yep, and I've tested even across multiple languages (but they give the same problem). Ofc I do believe the issue is mine

TelegramXPlus commented 1 year ago

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

ba0f3 commented 1 year ago

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

TelegramXPlus commented 1 year ago

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

ba0f3 commented 1 year ago

I hadn't done a webapp before, but there are to ways to get data from webapp:

TelegramXPlus commented 1 year ago

And 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.

TelegramXPlus commented 1 year ago

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?