Using Nuxt 3 server routes for telegram bot api

In this short article, we’re going to use telegram setWebhook, instead of getUpdates polling mechanism. I omit the steps where you have to set up a new bot in telegram UI, using BotFather

Nuxt 3 setup

Create telegram-webhook.post.ts in “ser…


This content originally appeared on DEV Community and was authored by Alexander Kim

In this short article, we're going to use telegram setWebhook, instead of getUpdates polling mechanism. I omit the steps where you have to set up a new bot in telegram UI, using BotFather

Nuxt 3 setup

Create telegram-webhook.post.ts in "server/api" directory with the following contents:

import TelegramBot from 'node-telegram-bot-api'

export default defineEventHandler(async event => {
  const config = useRuntimeConfig()
  const token = config.telegramBotToken

  // Create a new TelegramBot instance (without polling)
  const bot = new TelegramBot(token)

  // Read the incoming request body
  const body = await readBody(event)

  // Extract the message
  const { message } = body

  if (message) {
    const chatId = message.chat.id
    const text = message.text

    // Respond to the received message
    if (text) {
      await bot.sendMessage(chatId, `Ты сказал: ${text}`)
    }

    // Handle /start command
    if (text && text.startsWith('/start')) {
      await bot.sendMessage(
        chatId,
        'Hi, i am bot, how can i help you?',
      )
    }
  }

  return { status: 'ok', message }
})

don't forget to add "node-telegram-bot-api" npm package. The we start Nuxt3 instance. Now we need to set up a webhook.

We have to call:
curl https://api.telegram.org/bot<telegram_bot_token>/setWebhook?url=https://<your-domain.com>/api/telegram-webhook
this command will set up our POST API endpoint as webhook for telegram. Official documentation on hooks

If you're done everything corretly, then whenever you type a message to your bot, you'll get a response.


This content originally appeared on DEV Community and was authored by Alexander Kim


Print Share Comment Cite Upload Translate Updates
APA

Alexander Kim | Sciencx (2024-07-21T06:55:17+00:00) Using Nuxt 3 server routes for telegram bot api. Retrieved from https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/

MLA
" » Using Nuxt 3 server routes for telegram bot api." Alexander Kim | Sciencx - Sunday July 21, 2024, https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/
HARVARD
Alexander Kim | Sciencx Sunday July 21, 2024 » Using Nuxt 3 server routes for telegram bot api., viewed ,<https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/>
VANCOUVER
Alexander Kim | Sciencx - » Using Nuxt 3 server routes for telegram bot api. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/
CHICAGO
" » Using Nuxt 3 server routes for telegram bot api." Alexander Kim | Sciencx - Accessed . https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/
IEEE
" » Using Nuxt 3 server routes for telegram bot api." Alexander Kim | Sciencx [Online]. Available: https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/. [Accessed: ]
rf:citation
» Using Nuxt 3 server routes for telegram bot api | Alexander Kim | Sciencx | https://www.scien.cx/2024/07/21/using-nuxt-3-server-routes-for-telegram-bot-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.