Skip to content

Test script

Sahri Riza Umami edited this page May 24, 2017 · 6 revisions

tdcli.lua is a Work In Progress. Most of tdlib functions are not yet tested or not optimally tested.
Below is my test script. It's a modified version of example script from https://valtman.name/telegram-cli. If you have a better script or testing method, let me know.

  • Install luarocks

    sudo apt install luarocks
  • Install lua serpent module to pretty print the messages

    sudo luarocks install serpent
  • Place telegram-cli binary and this test script on a same directory.

    -- Look for lua 5.2 libraries on these PATH  
    package.path = package.path .. ';.luarocks/share/lua/5.2/?.lua'
      .. ';.luarocks/share/lua/5.2/?/init.lua'
    package.cpath = package.cpath .. ';.luarocks/lib/lua/5.2/?.so'
    
    -- Load the libraries
    serpent = require "serpent"
    -- dofile is prefered than require as we will edit some function on tdcli.lua to make it work.
    -- dofile need a PATH, except if this script is on the same directory level with the module we would like to import. 
    tdcli = dofile('tdcli.lua')
    
    -- Print message
    local function vardump(value)
      print(serpent.block(value, {comment=false}))
    end
    
    -- Print callback. See this result to see if our commands is working.
    function dl_cb(arg, data)
      print('=====================================================================')
      vardump(arg)
      vardump(data)
      print('=====================================================================')
    end
    
    function tdcli_update_callback(data)
      -- dumping all Telegram message structure
      vardump(data)
      
      if (data.ID == "UpdateNewMessage") then
        local msg = data.message_
        local input = msg.content_.text_
        local chat_id = msg.chat_id_
        local user_id = msg.sender_user_id_
        
        -- dumping Telegram message structure
        vardump(msg)
        
        -- if message is a text message
        if msg.content_.ID == "MessageText" then
          -- and the text is pang
          if input == "pang" then
            -- call a function
            tdcli.getActiveSessions()  
          elseif input == "peng" then
            tdcli.getAuthState()
          elseif input == "ping" then
            tdcli.getMe()
          elseif input == "pong" then
            tdcli.getSupportUser()
          elseif input == "pung" then
            tdcli.resetAuth()
          -- these lines below if we need to input a value into a function
          elseif input:match('^tas') then
            local text = input:gsub('tas', '')
            tdcli.getWebPagePreview(text)
          elseif input:match('^tes') then
            local text = input:gsub('tes', '')
            tdcli.sendMessage(chat_id, 0, 0, '<b>' .. text .. '</b>', 1, 'html')
          elseif input:match('^tis') then
            local text = input:gsub('tis', '')
            tdcli.changeAbout(text)
          elseif input:match('^tos') then
            local text = input:gsub('tos', '')
            tdcli.changeUsername(text)
          elseif input:match('^tus') then
            local text = input:gsub('tus', '')
            tdcli.changeAccountTtl(text)
          end
        end
      elseif (data.ID == "UpdateOption" and data.name_ == "my_id") then
        tdcli_function ({
          ID="GetChats",
          offset_order_="9223372036854775807",
          offset_chat_id_=0,
          limit_=20
        }, dl_cb, nil)
      end
    end

Test script
Frequently Asked Questions
The Functions

Clone this wiki locally