Telegram method of interacting with multiple people independently

Hello everyone!

I wanted to try to get this working, as I currently have a slew of commands that work with my own hard-coded Telegram ID, although I have multiple people in my household, and occasionally a family member that visits and I’d like to try to implement a multiple-choice menu system via Telegram for some basic commands of primarily used lights and routines… (I have several implemented for me, either reminders when a certain thing is triggered with buttons that I can tap to select, or just warnings if certain doors aren’t latched shut etc, all of which works fine… just for me…)

I came up with the idea of a potential way to do this last night, but am having a hard time getting it to work so I’m hoping someone can help. I’m sure it’s due to the way I’m creating and passing variables.

All applicable Telegram ID’s have been added under my things file, so the bot will only accept messages from trusted users.

Under my telegram.rules file (I have about 650+ lines in it, but have snipped it down to just my concept code for interacting with multiple users.)

//Initiate variables
var String commandreplyid = null
var String commandreplyname = null

rule "Telegram Bot Commands"
when
    Item telegramMessage received update
then

    val telegramAction = getActions("telegram","telegram:telegramBot:Telegram_Bot")

    var String botCommand = telegramMessage.state.toString.toLowerCase

    //Hard-code the commandreplyid and commandreplyname based upon the most recent MessageUser.state
    if(Telegram_MessageUser.state == "Username1"){
        commandreplyid = "123456789"
        commandreplyname = "Nickname1"
    }
    if(Telegram_MessageUser.state == "Username2"){
        commandreplyid = "024680246"
        commandreplyname = "Nickname2"
    }
    if(Telegram_MessageUser.state == "Username3"){
        commandreplyid = "135791357"
        commandreplyname = "Nickname3"
    }
    if(Telegram_MessageUser.state == "Username4"){
        commandreplyid = "987654321"
        commandreplyname = "Nickname4"
    }
    logInfo("telegram.rules", commandreplyid+", Name: "+commandreplyname+" Messaged.")

    if (botCommand == "help"){
        //Reply to user using previously hard-coded commandreplyid and commandreplyname
        telegramAction.sendTelegram(commandreplyid, "Hi "+commandreplyname+"!  Available Options: ...")

    } else if (botCommand == "test"){

        //Additional commands and actions, etc

    } 

    //Reset variables back to null
    commandreplyid = null
    commandreplyname  = null

end

the logInfo() bit echos the hard-coded variables commandreplyid and commandreplyname in the logfile no problem from the specified user who sends a message, but the telegramAction.sendTelegram() does not execute properly, and instead of saying “Hi Nickname1! Available Options: …” its just sending a single message of the user’s commandreplyid to all users.

It’s like the commandreplyid variable isn’t passing as a string to the telegramAction.sendTelegram() function, so instead of sending the specified message, it’s just sending the users id to everyone.

I’m stumped and can’t figure that part out.

Looking to the telegram binding documentation I see two different commands:
a) telegramAction.sendTelegram(1234567L, "Hello world!")
b) telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, lights are *off* now.")

The first one ( a ) does not use a string. It is the chat id formatted as a long value.
Teh second one ( b ) is a string and it uses the ReplyId to an earlier send question to the user. Which I think is not the chatid:

Sends a message after the user has answered a question. You should always call this method after you received an answer. It will remove buttons from the specific question and will also stop the progress bar displayed at the client side. If no message is necessary, just pass null here.

Hello and thanks for the reply,

So for a lot of other code I do use the sendTelegramAnswer to confirm selections, but that’s used after you have the bot send a message using sendTelegramQuery (Either on a timer, or an initial command to the bot which then triggers the sendTelegramQuery, and then have a listener for the answer to which it replies back using sendTelegramAnswer to confirm your selection.

In this case, I’m mainly after option a) for starters. It works if I hard-define the ID. (Using 123456789 as an example id)

If I hardcode the id in:

    if (botCommand == "help"){
        telegramAction.sendTelegram(123456789L, "Hi "+commandreplyname+"!  Available Options: ...")
    }

Then send “help” I immediately get a reply back to that particular account “Hi Nickname1! Available options…” So I can see it’s passing the nickname I’m assigning via “commandreplyname”. But I don’t want all replies from 4 different users to go to just my account.

So ultimately I’d like that 123456789L to be defined in the previous bits matching the username of who is messaging, just so I don’t have to either 1. duplicate the if/else user block within every single command code for Telegram, or 2. duplicate every command 4 times, 1 for each hard-coded user id. This would allow me to just define the users who will be interacting once, and have all the potential bot commands below it.

I tried changing the initial variable

var String commandreplyid = null to
var Number commandreplyid = null

But that makes no difference, so I guess my issue is formatting commandreplyid as a long value. Can you provide an example of how I can do that via setting it as a variable for this bit of code:

    if(Telegram_MessageUser.state == "Username1"){
        commandreplyid = "123456789"
        commandreplyname = "Nickname1"
    }
    if(Telegram_MessageUser.state == "Username2"){
        commandreplyid = "024680246"
        commandreplyname = "Nickname2"
    }
    if(Telegram_MessageUser.state == "Username3"){
        commandreplyid = "135791357"
        commandreplyname = "Nickname3"
    }
    if(Telegram_MessageUser.state == "Username4"){
        commandreplyid = "987654321"
        commandreplyname = "Nickname4"
    }

I’ve removed the quotes but that makes no difference and I’m unable to much about long values besides defining the variable as a number.

Appreciate the input :slight_smile:

Try:

val long commandreplyid = 123456789

This is how to send to a “long” (chat) id. Guess that’ll work in replies, too.

var long id = Long::parseLong(TelegramChatID.state.toString)
telegramAction.sendTelegram(id, msg)

Ahhh, lower case on long, I noticed how String, Number, Timer, all the things I’ve used before changed color using an uppercase, but Long wasn’t changing color in me in Visual Studio Code. Defining the variable with “long” worked while removing the quotes from the commandreplyid variables.

Much appreciated!

1 Like

Hi Markus,

Thank you that’s actually a nicer way to format things as well, I appreciate your reply and definitely find that helpful!

1 Like