Pass string to interactive linux cli program using script

I am using a library to access telegram via command line in bash linux ubuntu. This is the Kli telegram. LINK https://github.com/vysheng/tg

It is an interactive program that can be run

./bin/telegram-cli -k tg-server.pub

      

It outputs the following lines:

Telegram-cli version 1.1.1, Copyright (C) 2013-2014 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
I: config dir=[/home/utonto/.telegram-cli]
>

      

With the ">" prompt. To send a message, in this request you need to enter

msg USERNAME This is a test message

      

I would like to make a script on my computer telling me the room temperature when I am at work. How can i do this? I searched SO and found a similar question. It was recommended to use the "expect" command, I did it, but it didn't work. Maybe, of course, because I'm a beginner. I have also tried

echo "msg USERNAME this is a message |
telegram-cli -k ......"

      

but it didn't work. As far as you know, on my android I have a phone number that I use for telegram, but I signed up for telegram-cli with another phone. Can anyone help me?

+3


source to share


3 answers


See here: Remote Control with Telegram

To intercept a new incoming message, we create an action.lua file

"Lua is a powerful, fast, lightweight, embedded scripting language.

Lua combines simple procedural syntax with a powerful description of these constructs based on associative arrays and extensible semantics. Lua is dynamically typed, executed by bytecode interpretation for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping. "

From http://www.lua.org .

sudo nano /home/pi/tg/action.lua

      

with this content



function on_msg_receive (msg)
    if msg.out then
        return
    end
    if (msg.text=='ping') then
        send_msg (msg.from.print_name, 'pong', ok_cb, false)
    end
end

function on_our_id (id)
end

function on_secret_chat_created (peer)
end

function on_user_update (user)
end

function on_chat_update (user)
end

function on_get_difference_end ()
end

function on_binlog_replay_end ()
end

      

Save and exit when the incoming text message is "ping", Telegram responds with a text message containing "pong".

move in tg

cd /home/pi/tg

      

Then enter

bin/telegram-cli -k tg-server.pub -W -s action.lua

      

+4


source


Try the doc here:

#!/bin/bash
./bin/telegram-cli -k tg-server.pub <<-EOF
msg USERNAME This is a test message
exit
EOF

      



I added exit - don't know if needed. NOTE. The EOF element can be any group of letters that the shell cannot interpret as a command. Before the first EOF, it should be reported here that the next EOF is not in the leftmost column. If you omit it, then the last EOF should be as far away as you can go. Column # 1.

As for getting the temperature, I don't know how you would do it without special equipment.

+1


source


If you have some way to get the temperature on your computer, then sending it to bash is trivial enough for you. The correct way to handle a repetitive task is cron

. You would create an entry crontab

to run your script (or direct the temperature itself to a telegram). Format for cron entry

:

* * * * * command_to_execute
| | | | |
| | | | +- day of week  (0-6) (Sunday = 0)
| | | +--- month        (1-12)
| | +----- day of month (1-31)
| +------- hour         (0-23)
+--------- minute       (0-59)

      

In your case, you can install crontab

with crontab -e

(to edit crontab entries). To send the temperature every 10 minutes it would be:

*/10 * * * * ./script/to/call

      

or you can try to call the telegram right in yours crontab

with something like:

*/10 * * * * echo "msg $USER The current temp is: $temperature" | ./bin/telegram-cli -k tg-server.pub

      

Finally, check the parameters in telegram-cli

, it might be pretty good that the message takes as an argument and forwards it altogether, avoiding interactive mode (I haven't tested).

0


source







All Articles