Fork me on GitHub

目录:

如果你的树莓派不能通过路由器端口转发直接从家庭网络外访问,你还可以用类似聊天App一样的方式来和防火墙后的树莓派轻松交互,通过命令来控制树莓派。

Telegram

image

Telegram是一个跨平台的即时通讯软件,客户端和通讯及加密协议完全公开且开源。官方有正式发布Android,iOS,Mac OS X, Web等客户端版本;Telegram允许多端同时登录。我们在手机上安装Telegram客户端,同时在树莓派上用同一个帐号或另一个帐号也登录Telegram,那么就可以用手机和树莓派聊天了,更高级的一些用法可以是把树莓派帐号加到群聊中,实现类似微软小冰的功能;也可以由树莓派主动向你的手机Push消息实现提醒功能。

安装telegram-cli

Linux的Telegram客户端telegram-cli源代码在 https://github.com/vysheng/tg,按照Readme在树莓派上编译好。telegram-cli支持消息事件对lua脚本中的函数进行回调,支持的函数列表在:https://github.com/vysheng/tg/blob/master/README-LUA,也支持外部程序通过telegram-cli侦听的端口发送交互命令(一次连接只能一个命令)

Lua交互脚本

脚本命名为:tg_raspberrypi.lua

now = os.time()

chat = "树莓派通知" -- telegram的会话名称

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(safe_commands[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"))

启动telegram-cli

第一次启动

第一次启动telegram-cli会需要你输入手机号码,输入短信验证码后登录,成功登录后会在~/.telegram-cli下保存登录信息,后面就不需要再登录了。

以后台方式启动

./telegram-cli -k server.pub -W -D -P 7777 -d -s tg_raspberrypi.lua &

以上命令会打开7777端口接收telegram-cli命令,本机的其他程序可以调用下列命令在”树莓派通知”这个对话中发送一条新消息。

echo -e "msg 树莓派通知 hello" | nc localhost 7777` 

如果是其他机器则可以使用socat端口转发下

手机设置

在手机上登录Telegram后,创建一个新的会话,命名为“树莓派通知”,并把树莓派上登录的Telegram帐号加进来,就可以聊天了。。。连接上Webcam后,你还可以发送一条消息让树莓派启动摄像头拍一张照片回复给你。

参考连接

  1. http://zh.wikipedia.org/wiki/Telegram
  2. https://telegram.org


blog comments powered by Disqus

Published

2015-02-27

Categories


Tags