Logic Machine Forum
TELEGRAM APP - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: TELEGRAM APP (/showthread.php?tid=2666)

Pages: 1 2


TELEGRAM APP - Hosutech - 01.06.2020

Good morning,

there is the possibility of transmitting events with this application (e.g. tripping protections, exceeded threshold, ..)


RE: TELEGRAM APP - Daniel - 01.06.2020

It was explained here https://forum.logicmachine.net/showthread.php?tid=427&pid=2513#pid2513 but lets make a clear version:

1. Install Telegram app

2. Find BotFather or go here https://t.me/botfather

3. Once in BotFather type /newbot and follow instructions. As a result you will get your token and your own bot link like t.me/yourname_bot Click on the link and type any message there

4. Open browser (preferably Firefox as it converts JSON automatically) and go here https://api.telegram.org/bot<token>/getUpdates. Replace <token> with your token. As a result you should have something like that. Save your chat ID
   

5. Create a user library called telegram. paste this code and replace your token and chat ID
Code:
require('ssl.https')

local token = 'token' -- your token
local chat_id = '1234567' -- your chat id

function telegram(message)
  local url = 'https://api.telegram.org/bot' .. token .. '/sendMessage'
  local data = 'chat_id=' .. chat_id .. '&text=' .. socket.url.escape(message)
  return ssl.https.request(url, data)
end

6. Create an event based script and use this code to send message to Telegram app:
Code:
require("user.telegram")
message = 'test message'
res, err = telegram(message)
log(res, err)



RE: TELEGRAM APP - JRP - 03.06.2020

Hello
First of all thank you for your summary, precisely in the indicated thread I put a comment requesting a clear guide like yours.

I have done all the steps indicated and it doesn't work for me.

If I manually assemble the url to send messages and paste it in firefox, the message is sent and appears in telegram.

If I put the same token and the same chat id in the user script and create the event script, nothing happens when executing it.

I have modified the user script with a log, in addition to the one present in the event script. This is the result.

This is the user script code
Code:
require('ssl.https')

local token = '110xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' -- your token
local chat_id = '-23xxxxxxxx' -- your chat id

function telegram(message)
  local telegram_url = 'https://api.telegram.org/bot' .. token .. '/sendMessage?'
  message=socket.url.escape(message)
  local data_str = 'chat_id=' .. chat_id .. '&text=' .. message..''
  local res, code, headers, status = ssl.https.request(telegram_url, data_str)

log(telegram_url, message, data_str, res, code, headers, status, ssl.https.request)
end
This is the event script
Code:
require("user.telegram")

message = 'test message'

res, err = telegram(message)

log(res, err)
Thank you.

a greeting


RE: TELEGRAM APP - Daniel - 03.06.2020

what fw do you use?


RE: TELEGRAM APP - JRP - 03.06.2020

(03.06.2020, 18:02)Daniel. Wrote: what fw do you use?
Hello
Thank you for your interest.
I have a wiser with version 2.4.0
a greeting


RE: TELEGRAM APP - Daniel - 03.06.2020

It will work in new FW 2.5.0 which should be available in a month time.

For older fw it will work like this

Code:
function request(url)
  local resp = {}
  local stat, code, hdrs = require('ssl.https').request({
    method = 'GET',
    url = url,
    protocol = 'tlsv12',
    sink = ltn12.sink.table(resp),
  })

  if stat then
    stat = table.concat(resp)
  end

  return stat, code, hdrs
end

local chat_id = '12345'
local token = 'wwwwwwwwwwwwwwwwwwwwwwwwww'
local telegram_url = 'https://api.telegram.org/bot' .. token .. '/sendMessage?'


message = "test"
message=require('socket.url').escape(message)
data_str = 'chat_id=' .. chat_id .. '&text=' .. message..''
stat, code, hdrs = request(telegram_url .. data_str)



RE: TELEGRAM APP - JRP - 03.06.2020

Hello
Thanks for the reply.

I'll wait for the update to launch.

Out of curiosity, why is it not working?
a greeting


RE: TELEGRAM APP - Erwin van der Zwart - 03.06.2020

Hi,

Because there was a change in the http library that is included in the new FW..

BR,

Erwin


RE: TELEGRAM APP - admin - 04.06.2020

Older firmware uses SSLv3 for HTTPS requests by default. SSLv3 is disabled on most servers for security reasons so TLSv1.2 or newer must be used instead. HTTPS library in the new FW will use TLSv1.2 by default. The updated code that Daniel posted is perfectly fine and will work on all firmware versions.


RE: TELEGRAM APP - JRP - 04.06.2020

Hello
Thank you all for answering me.
It is interesting to know.
a greeting


RE: TELEGRAM APP - baggins - 04.06.2020

(04.06.2020, 04:36)admin Wrote: Older firmware uses SSLv3 for HTTPS requests by default. SSLv3 is disabled on most servers for security reasons so TLSv1.2 or newer must be used instead. HTTPS library in the new FW will use TLSv1.2 by default. The updated code that Daniel posted is perfectly fine and will work on all firmware versions.

I tried this code on firmware 20160714 and it fails:


Code:
log("code: " ..tostring(code))
log("headers: " ..tostring(hdrs))
log("status: " ..tostring(stat))

test_telegram 04.06.2020 11:05:19
* string: status: nil
test_telegram 04.06.2020 11:05:19
* string: headers: nil
test_telegram 04.06.2020 11:05:19
* string: code: revcd alert fatal error



RE: TELEGRAM APP - admin - 04.06.2020

20160714 is too old


RE: TELEGRAM APP - JRP - 11.07.2020

(01.06.2020, 14:29)Daniel. Wrote: It was explained here https://forum.logicmachine.net/showthread.php?tid=427&pid=2513#pid2513 but lets make a clear version:

1. Install Telegram app

2. Find BotFather or go here https://t.me/botfather

3. Once in BotFather type /newbot and follow instructions. As a result you will get your token and your own bot link like t.me/yourname_bot Click on the link and type any message there

4. Open browser (preferably Firefox as it converts JSON automatically) and go here https://api.telegram.org/bot<token>/getUpdates. Replace <token> with your token. As a result you should have something like that. Save your chat ID


5. Create a user library called telegram. paste this code and replace your token and chat ID
Code:
require('ssl.https')

local token = 'token' -- your token
local chat_id = '1234567' -- your chat id

function telegram(message)
  local telegram_url = 'https://api.telegram.org/bot' .. token .. '/sendMessage?'
  message=socket.url.escape(message)
  local data_str = 'chat_id=' .. chat_id .. '&text=' .. message..''
  local res, code, headers, status = ssl.https.request(telegram_url, data_str)
end

6. Create an event based script and use this code to send message to Telegram app:
Code:
require("user.telegram")

message = 'test message'

res, err = telegram(message)

log(res, err)
Thanks Daniel, with the Wiser 2.5.0 update, this script works perfectly and the instructions are very clear.

Thank you all so much for your help.


RE: TELEGRAM APP - phongvucba - 04.08.2020

Hi everybody ! I have shortened the following code to send a message! But it didn't work, I used LM5 RIO2E version 20170620, it still started at first, it doesn't work anymore.
I have tried the telegram bot on the web browser and it still works.
Is there any wrong code in my code?
Please everyone help. !
----------------------------
http= require('ssl.https')
socket.http.timeout = 10
noidung = 'Báo%20động%20có%20người%20đột%20nhập%20qua%20cửa%20sổ!'
local ip=http.request('https://api.telegram.org/bot.../sendmessage?chat_id=-731524114&text='..noidung..'')
log('dfdgdfgdfgdfg=' .. ip)
----------------------


RE: TELEGRAM APP - Daniel - 04.08.2020

Install new fw and use example above.


RE: TELEGRAM APP - khalil - 04.03.2021

(01.06.2020, 14:29)Daniel. Wrote: It was explained here https://forum.logicmachine.net/showthread.php?tid=427&pid=2513#pid2513 but lets make a clear version:

1. Install Telegram app

2. Find BotFather or go here https://t.me/botfather

3. Once in BotFather type /newbot and follow instructions. As a result you will get your token and your own bot link like t.me/yourname_bot Click on the link and type any message there

4. Open browser (preferably Firefox as it converts JSON automatically) and go here https://api.telegram.org/bot<token>/getUpdates. Replace <token> with your token. As a result you should have something like that. Save your chat ID


5. Create a user library called telegram. paste this code and replace your token and chat ID
Code:
function telegram(message)
  local url = 'https://api.telegram.org/bot' .. token .. '/sendMessage'
  local data = 'chat_id=' .. chat_id .. '&text=' .. socket.url.escape(message)
  return ssl.https.request(url, data)
end

6. Create an event based script and use this code to send message to Telegram app:
Code:
require("user.telegram")

message = 'test message'

res, err = telegram(message)

log(res, err)

hello 
I use SL V2.6.0 
I follow the steps but I got this error:

User library telegram:6: attempt to index global 'socket' (a nil value)
stack traceback:
User library telegram:6: in function 'telegram'
User script:5: in main chunk



RE: TELEGRAM APP - admin - 04.03.2021

You are missing this before function telegram(message):
Code:
require('ssl.https')

local token = 'token' -- your token
local chat_id = '1234567' -- your chat id



RE: TELEGRAM APP - khalil - 04.03.2021

(04.03.2021, 10:38)admin Wrote: You are missing this before function telegram(message):
Code:
require('ssl.https')

local token = 'token' -- your token
local chat_id = '1234567' -- your chat id

thanks 
you are rigt I forget this: require('ssl.https')  Rolleyes


RE: TELEGRAM APP - khalil - 04.03.2021

Hello
is it possible to send csv file to telegram?


RE: TELEGRAM APP - admin - 04.03.2021

See this example: https://forum.logicmachine.net/showthread.php?tid=2792&pid=17944#pid17944
Replace name/filename as needed, set ctype to text/csv and finally replace /sendPhoto with /sendDocument in the URL.