skip to Main Content

I’m trying to create a small app that reads a chat message and echoes a filtered text

I need it to take this message:

INPUT:

#notthis/remove

remove this line

This one too

Output:

notthis

At the moment all it does is remove the second word + the first hashtag:

"hello#hello" which becomes hello

I tried adding the / like this input.split("#","/"); but all it does is crash the program.

Not asking to do the program for me, but I’d really appreciate any hints.
Thank you!

const Telegraf = require('telegraf');

const bot = new Telegraf('182049');

const helpMessage = `
Say something to me
/start - start the bot
/help - command reference
`;

bot.start((ctx) => {
    ctx.reply("Hi I am echoo bot");
    ctx.reply(helpMessage);
});

bot.help((ctx) => {
    ctx.reply(helpMessage)
});


bot.on("text", (ctx) => {
    let input = ctx.message.text;
    let inputArray = input.split("#");

    console.log(inputArray);
    
    let message = "";

    if (inputArray.length == 1) {
        message = "no separator";
    } else {
        inputArray.shift();
        message = inputArray.join(" ");
    }
    
    ctx.reply(message);
});


bot.launch()

2

Answers


  1. Chosen as BEST ANSWER

    I think I made it

    const Telegraf = require('telegraf');
    
    const bot = new Telegraf('18204DjYLa9o9Y');
    const regex = /#w+//g;
    
    const helpMessage = `
    Say something to me
    /start - start the bot
    /help - command reference
    `;
    
    bot.start((ctx) => {
        ctx.reply("Hi I am echoo bot");
        ctx.reply(helpMessage);
    })
    
    bot.help((ctx) => {
        ctx.reply(helpMessage)
    })
    
    
    bot.on("text", (ctx) => {
        let input = ctx.message.text;
        
        let inputArray = input.split("#");
        
        console.log(inputArray);
        
    
        let message = "";
    
       
        inputArray.shift();  
        message = inputArray.join(" ");
            
        let messageArray =  message.split("/");
        console.log(messageArray);
       
        for(let index = 0; message[index] != '/'; index++ )
        {
            ctx.reply("/ta " + message[index]);
        }
        return null
    
        
      })
    
    
    
    
      bot.hears("cat", (ctx) =>{
        ctx.reply("Meow");
    })
    
    
    bot.launch()
    
    

    Only a little issue: if my coin has more than 3 letters it gets truncated. For loop solves the problem but every letter starts with a new line, and the second imput gets messed up

    Eg:   echobot, [13.05.21 00:54]
    E
    
    echobot, [13.05.21 00:54]
    O
    
    echobot, [13.05.21 00:54]
    S
    

    2nd attempt:

    
    
    echobot, [13.05.21 00:54]
    S
    
    echobot, [13.05.21 00:54]
    O
    
    echobot, [13.05.21 00:54]
    E
    

  2. Regular Expression (regex) is a great way to find substrings in an unknown string, even though it might be a bit daunting to work with. You can use this website to create your regex.

    To find a substring starting with # and ending with /, you can use the following expression:

    /#w+//g
    

    This will match 1 or more word characters that are in between a # and a /.

    For example:

    • #foo/ will match
    • #f/ will match
    • #/ will not match
    • #foo bar/ will not match (whitespace is not a word character)

    An example JavaScript code:

    const regex = /#w+//g;
    
    const text = "something#fooo/bar #lorem/ipsum";
    const found = text.match(regex);
    
    // For each found item, remove the first and last characters
    const cleaned = found.map((a) => a.slice(1, -1));
    
    console.log(cleaned);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search