1

My msg.text = /start 76198769
/start + space + 76198769

How Can I Make a Condition if msg.text = /start+76198769 then Temp = 76198769 ?

I Mean, I want to get 76198769

await bot.sendMessage(msg.chat.id, `telegram.me/myBot?start=${msg.chat.id}`, opts);
1
  • Fix your syntax first please. Commented Oct 25, 2017 at 0:49

4 Answers 4

3

There's a few ways to do it. Simplest is to probably just split your string.

let vals = msg.text.split(' '); // yields an array of '/start', '76198769'
let temp = null;
if(vals[0] === '/start' && vals[1] === '76198769') { 
  temp = parseInt(vals[1]);
}

Or with Regex, you could do

let matches = /\/start\s(\d+)/g.exec(msg.text);
let temp = null;
if(matches.length) {
  temp = parseInt(matches[1]);
}

Just for fun, I'd point out that what I'm recommending here could be extended as well. For example, if you have a list of commands you want to use, you could test for them and then route the command to some other code.

Let's say you have commands like 'start', 'stop', 'info'. If you create an object (perhaps through requires in Node) like this:

const commands = {
  'start' : function(args) { /* handle start */},
  'stop' : function(args) { /* handle stop */},
  'info' : function(args) { /* handle info */}
};

You can construct your regex and fetch it like this:

const regex = /\/(start|stop|info)\s(\d+)/g;
let command = null;
let args = null;
let matches = regex.exec(msg.text);
if(matches.length) {
  command = matches[1];
  args = parseInt(matches[2]);
}

Then you can execute your command with commands[command](args);

If you really want, you can even construct your regex from a string by concatenating the keys of the commands object, but I'll leave that as an exercise to you. :)

Sign up to request clarification or add additional context in comments.

3 Comments

vals[1] is a variable number, if that number change, Will the code work?
If vals[1] can be any number at all, then just remove the second condition in the if, to make it if(vals[0] === '/start') {... }.
First one is ok But with regex , I'm Using ``` let matches = msg.text.match(/\/start\s(\d+)/g); let temp = null; if(matches.length) { temp = parseInt(matches[0]); } console.log(temp);``` But I Got `NaN
3

Use Regex:

var matches = msg.text.match(/(\d+)/g);
if (matches.length > 0) {
    var Temp = parseInt(matches[0]);
}

Example here.

3 Comments

That's going to match a lot more than he wants, potentially. Also it makes Temp a string, and his example was a number.
@Paul The example he gave has nothing to suggest there would be other numbers in the same string. But if that's the case, you can easily modify the regex accordingly.
I agree with Paul, your awnser worked but Paul Awnser is better because your awnser will work with /anothercommand anyNumber, I awsered too I prefer my awnser because is more fit and just work with /start number , forexample not show error NaN in /start aaaaa or when we have olny /start, that just work when we have /start and after that only a numbers. Thank you
2

You can use split()...

const temp = msg.text === '/start 76198769' ? msg.text.split(' ')[1] : '';

or with regx:

var matches = msg.text.match(/\d+/g), temp;
temp = matches.length ? matches[0] : '';

Comments

1

This work Very Good
Just This work with /start and Numbers

    let vals = msg.text.split(' '); 
    let temp = null;
    if (vals[0] === '/start' && Math.round(vals[1])) {
        temp = parseInt(vals[1]);
        console.log(temp); 
    }

1 Comment

This code won't even execute without errors; you're re-assigning a let constant for starters, and rounding a string for no reason, then parsing it (again, since the round function parses it as well).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.