• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoLinux ApplicationsUse Telegram to Interact with your pcDuino3
Previous Next

Use Telegram to Interact with your pcDuino3

Posted by: admin , March 4, 2015

Telegram is a messaging app with a focus on speed and security, it’s super fast, simple and free. You can use Telegram on all your devices at the same time — your messages sync seamlessly across any of your phones, tablets or computers.

With Telegram, you can send messages, photos, videos and files of any type (doc, zip, mp3, etc), as well as create groups for up to 200 people. You can write to your phone contacts and find people by their usernames. As a result, Telegram is like SMS and email combined — and can take care of all your personal or business messaging needs.

t_logo

 

 

In this post, we will show how to use Telegram to interact with your pcDuino.  Telegram has the clients covering Android, iOS, and web. We can install Telegram on your iPhone by search for ‘telegram’ in Apple app store.

The source files of the linux Telegram client ‘telegram-cli’ are hosted at: https://github.com/vysheng/tg . We can install it on pcDuino by following the instructions on the Readme file.

A quick recap here:

1. First, we do ‘$sudo apt-get update’ to update the package source.

You may get a warning syaing that “W: conflicting distribution”. Ignore this.

2. Install ‘git’ by ‘$sudo apt-get install git’.

3. Clone github repo:

 

$git clone --recursive https://github.com/vysheng/tg.git && cd tg

4. Install build tool:

$sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev make

5. Then we build by:

$./configure
$make

6. We create a file named ‘tg_pcduino.lua’ with the following content:

now = os.time()
chat = "pcDuinomessage" -- name of telegram's chat 
safe_commands = {}
safe_commands["uptime"]  = "uptime"
safe_commands["w"]       = "w"
safe_commands["ps"]      = "ps ax"
safe_commands["netstat"] = "netstat -na"
safe_commands["df"]      = "df"
safe_commands["ss"]      = "ss"
safe_commands["free"]    = "free"
function on_msg_receive (msg)
    if msg.out then
        return
    end
    if msg.text then
        -- mark_read(msg.from.print_name)   
        -- vardump(msg)
        cmd = string.lower(trim(msg.text))
        if cmd == "ping" then
            send_msg (chat, 'pong', ok_cb, false)
        elseif safe_commands[cmd] ~= nil then
            send_msg (chat, exec(cmd), ok_cb, false)
        end
    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
function exec(cmd)
   local output = ""
   f = assert (io.popen (cmd))
   for line in f:lines() do
     output = output .. "\n" .. line
   end -- for loop
   f:close()
   return output
end 
function trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function vardump(value, depth, key)
  local linePrefix = ""
  local spaces = ""
  
  if key ~= nil then
    linePrefix = "["..key.."] = "
  end
  
  if depth == nil then
    depth = 0
  else
    depth = depth + 1
    for i=1, depth do spaces = spaces .. "  " end
  end
  
  if type(value) == 'table' then
    mTable = getmetatable(value)
    if mTable == nil then
      print(spaces ..linePrefix.."(table) ")
    else
      print(spaces .."(metatable) ")
        value = mTable
    end     
    for tableKey, tableValue in pairs(value) do
      vardump(tableValue, depth, tableKey)
    end
  elseif type(value)    == 'function' or 
      type(value)   == 'thread' or 
      type(value)   == 'userdata' or
      value     == nil
  then
    print(spaces..tostring(value))
  else
    print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
  end
end
print(exec("uptime"))

tg_pcduino.lua should be saved into the directory named ‘/home/ubuntu/tg/bin’.  Copy ‘server.pub’ from /home/ubuntu/tg to /home/ubuntu/tg/bin.

The file directory will look as below:

ubuntu@ubuntu:~/tg/bin$ ls
generate  server.pub  telegram-cli  tg_pcduino.lua  tl-parser
ubuntu@ubuntu:~/tg/bin$

 

Then we use the following command to start the process:

ubuntu@ubuntu:~/tg/bin$ sudo ./telegram-cli -k server.pub -W -D -P 7777 -d -s tg_pcduino.lua &

 

 

It will ask for phone number and then a code that SMSed to you.

Other process on the same pcDuino can send message out using:

$echo –e “msg  pcDuinomessage hello” | nc localhost 7777

Share!
Tweet

admin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors