Search code examples
javascripttelegramtelegram-botnode-telegram-bot-api

Telegraf framework. how to Forward message to a given group, chat or channel with telegram.forwardMessage();?


The problem: I need to get messages from aprox 400 users to a telegram bot. the bot hears for specific tags in the message and routes the messages to a group channel or user defined by the tag that the bot hears. when the final destination replys the bot needs to know where to forward that reply to achieve 2 way communication (one way is user to a whole department in a groupchat and the other way if from the department who answers only to the user)

so with this background I decided to use Telegraf Framework to build the bot. i'm new to JS and all relative technologies but it seems to be the best choice for this task. I'm trying to use the telegram.forwardMessage(); method but I'm not sure exactly how to populate the arguments the method needs.

the telegraf documentation shows

telegram.forwardMessage(chatId, fromChatId, messageId, [extra]) => Promise

but can't find any example of a practical use of the method

const Telegraf = require('telegraf');   // Module to use Telegraf API.
const {Extra, Markup} = Telegraf;   // Extract Extra, Markups from Telegraf module.
const config = require('./config'); // Configuration file that holds telegraf_token API key.

const bot = new Telegraf(config.mon_telegraf_token) //this line extracts the variable where the api token is stored
bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me message with the destination #TAG '))
bot.hears('hi', (ctx) => ctx.reply('Hey there')) //just playing around 
bot.hears('#RH', (ctx) => ctx.telegram.forwardMessage()) //aditionally need to reply to the user a confirmation that the message sent correctly and can't send a new message in 5 minutes


Solution

  • an example would be

    ctx.telegram.forwardMessage(whereToSendId, fromWhereToSendId, whatIdToSend).then(function(){
    console.log("mesage forwaded")
    });
    

    in your case fromWhereToSend would be ctx.message.chat.id and the whatIdToSend would be ctx.message.message_id

    The documentation is quite clear and you can check the official telegram documentation for more