Wednesday 15 August 2018

[Question] About Telegram (Telegraf) + Object inheritance

Greetings,I am making a Telegram bot using Telegraf.js framework.The architecture I am using is the following:- index.js is where the telegraf.js is initialized and when the bot receives a Telegram message an instance of the object ctx is initialized. To reply to the message we must call ctx.reply("string").- folder /replies/SOMESCRIPT.js is where I plan on developing all the replies/commands of the bot. I have designed it this way so when I want to add/delete/modify a module - all I have to do is to edit or create a file there and everything will be loaded dynamically on each message that the bot receives.- every reply/command must have the "exports.main" function which will do stuff and return the answer that the bot will give.A sample code from index.js:let currModule = require(repliesDir + replyName + '.js'); ctx.reply(currModule.main(ctx.message['text'])); // This is in a try/catch if the require fails it replies that there is no such command. A sample code from hi.js:exports.main = function(message) { return ("Hello!"); }; Everything works great, but the problem comes when I want to return something different than a string. For example - Telegraf uses ctx.replyWithPhoto to reply with a photo, but I am calling the ctx.reply in index.js and there is no way that index.js can know what type of answer will the other script's main function return.I was thinking of passing the ctx object instance in some way to hi.js so it could reply from there and this way index.js will be used just as a wrapper as it is supposed to be, but I can't thing of a way to do that.I do not have that much experience with Node/JS and I would like to get some recommendations on how to accomplish this architecture.

Submitted August 15, 2018 at 10:07PM by bilporti

No comments:

Post a Comment