test.lua 2.51 KB
Newer Older
Vysheng's avatar
Vysheng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
started = 0
our_id = 0

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 ("HI, this is lua script")

vvaltman's avatar
vvaltman committed
43 44
function ok_cb(extra, success, result)
end
Vysheng's avatar
Vysheng committed
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59
-- Notification code {{{

function get_title (P, Q)
  if (Q.type == 'user') then
    return P.first_name .. " " .. P.last_name
  elseif (Q.type == 'chat') then
    return Q.title
  elseif (Q.type == 'encr_chat') then
    return 'Secret chat with ' .. P.first_name .. ' ' .. P.last_name
  else
    return ''
  end
end

vvaltman's avatar
vvaltman committed
60 61 62 63 64 65 66 67 68 69 70
local lgi = require ('lgi')
local notify = lgi.require('Notify')
notify.init ("Telegram updates")
local icon = os.getenv("HOME") .. "/.telegram-cli/telegram-pics/telegram_64.png"

function do_notify (user, msg)
  local n = notify.Notification.new(user, msg, icon)
  n:show ()
end

-- }}}
71

Vysheng's avatar
Vysheng committed
72 73 74 75 76 77 78
function on_msg_receive (msg)
  if started == 0 then
    return
  end
  if msg.out then
    return
  end
vvaltman's avatar
vvaltman committed
79
  do_notify (get_title (msg.from, msg.to), msg.text)
vvaltman's avatar
vvaltman committed
80

Vysheng's avatar
Vysheng committed
81 82
  if (msg.text == 'ping') then
    if (msg.to.id == our_id) then
vvaltman's avatar
vvaltman committed
83
      send_msg (msg.from.print_name, 'pong', ok_cb, false)
Vysheng's avatar
Vysheng committed
84
    else
vvaltman's avatar
vvaltman committed
85
      send_msg (msg.to.print_name, 'pong', ok_cb, false)
Vysheng's avatar
Vysheng committed
86 87 88 89 90
    end
    return
  end
  if (msg.text == 'PING') then
    if (msg.to.id == our_id) then
Vysheng's avatar
Vysheng committed
91
      fwd_msg (msg.from.print_name, msg.id, ok_cb, false)
Vysheng's avatar
Vysheng committed
92
    else
Vysheng's avatar
Vysheng committed
93
      fwd_msg (msg.to.print_name, msg.id, ok_cb, false)
Vysheng's avatar
Vysheng committed
94 95 96 97 98 99 100 101 102
    end
    return
  end
end

function on_our_id (id)
  our_id = id
end

vvaltman's avatar
vvaltman committed
103 104
function on_user_update (user, what)
  --vardump (user)
Vysheng's avatar
Vysheng committed
105 106
end

vvaltman's avatar
vvaltman committed
107 108
function on_chat_update (chat, what)
  --vardump (chat)
Vysheng's avatar
Vysheng committed
109 110
end

vvaltman's avatar
vvaltman committed
111 112
function on_secret_chat_update (schat, what)
  --vardump (schat)
Vysheng's avatar
Vysheng committed
113 114 115 116 117
end

function on_get_difference_end ()
end

118 119 120 121 122
function cron()
  -- do something
  postpone (cron, false, 1.0)
end

Vysheng's avatar
Vysheng committed
123 124
function on_binlog_replay_end ()
  started = 1
125
  postpone (cron, false, 1.0)
Vysheng's avatar
Vysheng committed
126
end