Monday, January 26, 2015

automated telegram reply with centos

searching and i found this:
to send message:
http://stackoverflow.com/questions/24330922/sending-messages-with-telegram-apis-or-cli

to reply message:
http://stackoverflow.com/questions/27671801/pass-string-to-a-linux-cli-interactive-program-with-a-script
http://www.instructables.com/id/Raspberry-remote-control-with-Telegram/

First create a bash script for telegram called tg.sh:
#!/bin/bash
now=$(date)
to=$1
subject=$2
body=$3
tgpath=/home/youruser/tg
LOGFILE="/home/youruser/tg.log"
cd ${tgpath}
${tgpath}/telegram -k ${tgpath}/tg-server.pub -W <<EOF
msg $to $subject
safe_quit
EOF
echo "$now Recipient=$to Message=$subject" >> ${LOGFILE}
echo "Finished" >> ${LOGFILE}


To intercept a new incoming message we create a file action.lua
"Lua is a powerful, fast, lightweight, embeddable scripting language.
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping."
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 incoming text message is "ping", Telegram answers us with a text message containing "pong".
move in tg

cd /home/pi/tg
Then type
bin/telegram-cli -k tg-server.pub -W -s action.lua

No comments:

Related Posts Plugin for WordPress, Blogger...