This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

TELEGRAM APP
#30
How to send message from Telegram app to LM.

1. Fallow instruction in first post how to create bot and your TOKEN
2. Create user library tgupdates and paste this code
Code:
return function(token, callback)
  local json = require('json')
  local socket = require('socket')
  local ssl = require('ssl')
  local host = 'api.telegram.org'
  local tgupdateid = storage.get('tg_update_id')

  local uri = '/bot' .. token .. '/getUpdates'

  local sock = socket.tcp()
  sock:settimeout(5)

  local res, err = sock:connect(host, 443)

  if not res then
    sock:close()
    log('connect failed', err)
    os.sleep(5)
    return
  end

  sock = ssl.wrap(sock, { mode = 'client' })
  sock:settimeout(60)
  sock:sni(host)
  res, err = sock:dohandshake()

  if not res then
    sock:close()
    log('dohandshake failed', err)
    os.sleep(5)
    return
  end

  local function sendreq()
    local args = 'timeout=50'
    local crlf = '\r\n'

    if tgupdateid then
      args = args .. '&offset=' .. tgupdateid
    end

    sock:send(
      'POST ' .. uri .. ' HTTP/1.1' .. crlf ..
      'Host: ' .. host .. crlf ..
      'Content-Type: application/x-www-form-urlencoded' .. crlf ..
      'Content-Length: ' .. #args .. crlf .. crlf ..
      args
    )
  end

  local function parse(resp)
    resp = json.pdecode(resp)

    if type(resp) ~= 'table' or not resp.ok then
      log('invalid response', resp)
    end

    local update_id = 0

    for _, item in ipairs(resp.result) do
      update_id = math.max(update_id, item.update_id)

      local message = item.message
      local text = message.text
      local userid = message.from.id
      local username = message.from.first_name

      callback(text, userid, username)
    end

    if update_id > 0 then
      tgupdateid = update_id + 1
      storage.set('tg_update_id', update_id + 1)
    end
  end

  sendreq()
  local pat, len = nil, nil

  while true do
    res, err = sock:receive(pat)

    if err then
      sock:close()
      log('receive error', err)
      break
    end

    if type(pat) == 'number' then
      pcall(parse, res)
      sendreq()

      pat, len = nil, nil
    elseif #res == 0 then
      pat = len
    elseif not len then
      len = res:match('Content%-Length: (%d+)')
      len = tonumber(len)
    end
  end
end
3. Create resident script with 0 interval and use this code. Change TOKEN. Write to your chat in telegram and your message will be in logs.
Code:
function callback(text, userid, username)
  log(text, userid, username)
end

token = 'YOUR TOKEN HERE'
require('user.tgupdates')(token, callback)

Modify the function to run your actions based on revived text. UserID is a unique number which can be used for authorization/restrictions. username is not a unique as it is just name in your telegram account. It can be duplicated.
------------------------------
Ctrl+F5
Reply


Messages In This Thread
TELEGRAM APP - by Hosutech - 01.06.2020, 11:15
RE: TELEGRAM APP - by Daniel - 01.06.2020, 14:29
RE: TELEGRAM APP - by JRP - 03.06.2020, 17:18
RE: TELEGRAM APP - by JRP - 11.07.2020, 18:47
RE: TELEGRAM APP - by khalil - 04.03.2021, 10:36
RE: TELEGRAM APP - by olegrz - 04.09.2022, 12:32
RE: TELEGRAM APP - by Daniel - 03.06.2020, 18:02
RE: TELEGRAM APP - by JRP - 03.06.2020, 18:27
RE: TELEGRAM APP - by Daniel - 03.06.2020, 18:40
RE: TELEGRAM APP - by JRP - 03.06.2020, 18:57
RE: TELEGRAM APP - by Erwin van der Zwart - 03.06.2020, 21:06
RE: TELEGRAM APP - by olegrz - 06.01.2024, 17:51
RE: TELEGRAM APP - by olegrz - 07.01.2024, 09:09
RE: TELEGRAM APP - by admin - 04.06.2020, 04:36
RE: TELEGRAM APP - by baggins - 04.06.2020, 10:54
RE: TELEGRAM APP - by JRP - 04.06.2020, 07:35
RE: TELEGRAM APP - by admin - 04.06.2020, 11:17
RE: TELEGRAM APP - by phongvucba - 04.08.2020, 09:03
RE: TELEGRAM APP - by Daniel - 04.08.2020, 09:06
RE: TELEGRAM APP - by admin - 04.03.2021, 10:38
RE: TELEGRAM APP - by khalil - 04.03.2021, 10:44
RE: TELEGRAM APP - by khalil - 04.03.2021, 14:18
RE: TELEGRAM APP - by admin - 04.03.2021, 17:17
RE: TELEGRAM APP - by khalil - 08.03.2021, 14:28
RE: TELEGRAM APP - by admin - 09.03.2021, 07:08
RE: TELEGRAM APP - by khalil - 09.03.2021, 08:42
RE: TELEGRAM APP - by admin - 09.03.2021, 08:45
RE: TELEGRAM APP - by khalil - 09.03.2021, 09:24
RE: TELEGRAM APP - by edgars - 09.02.2022, 11:49
RE: TELEGRAM APP - by admin - 05.09.2022, 06:17
RE: TELEGRAM APP - by olegrz - 05.09.2022, 09:04
RE: TELEGRAM APP - by Daniel - 02.02.2023, 18:08
RE: TELEGRAM APP - by David - 26.04.2023, 07:28
RE: TELEGRAM APP - by admin - 26.04.2023, 07:28
RE: TELEGRAM APP - by David - 26.04.2023, 07:33

Forum Jump: