How to send a response from a server in JSON format to a Telegram bot in link format?

99 Views Asked by At

I have written a telegram bot using the grammy.js library and a web server using node.js and express.js.

The user launches the bot, enters his name and after that the telegram bot sends a GET request to the web server at “api/sign-in”. After this, the server generates an authentication request and returns it to the bot in JSON format.

Server code

app.get("/api/sign-in", async (req, res) => {
    console.log('get Auth Request');
    const deepLink = await GetAuthRequest(req, res)
    res.json({ deepLink })
});

onst scope = request.body.scope ?? [];
    request.body.scope = [...scope, proofRequest];
    requestMap.set(`${sessionId}`, request)
    const jsonString = JSON.stringify(request)
    const base64Request = Buffer.from(jsonString).toString('base64')
    const encodedRequest = encodeURIComponent(base64Request);
    const deepLink = `http://auth?request=${encodedRequest}`;
    return deepLink

The message is displayed in plain text that cannot be clicked or navigated to. How can I display the response from the server with a link so that I can click on it? The server console displays the message "get Auth Request". Accordingly, the request from the bot to the server is successful P.S. Maybe I chose the wrong library and grammy can't do that:

Telegram bot code

if (!userNames.has(chatId)) {
        userNames.set(chatId, msgText);
        await ctx.reply(`Welcome, ${msgText}!`);
        const data = await axios.get('http://localhost:8080/api/sign-in')
        .catch((error) => {
            console.error('ОError when receiving deep link:', error);
            ctx.reply('An error has occurred. Try later.');
            return;
        });
        if (data) {
            ctx.reply(data.data.deepLink);
            // ctx.reply(data.data.deepLink);
        } else {
            ctx.reply("null");
        }
    }

I expected the bot to display the response from the server in link format, but I always get a string format

0

There are 0 best solutions below