Posts: 45
Threads: 10
Joined: Jul 2017
Reputation:
0
Good morning,
there is the possibility of transmitting events with this application (e.g. tripping protections, exceeded threshold, ..)
Posts: 4638
Threads: 24
Joined: Aug 2017
Reputation:
207
It was explained here https://forum.logicmachine.net/showthrea...13#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)
------------------------------
Ctrl+F5
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
03.06.2020, 17:18
(This post was last modified: 03.06.2020, 17:19 by JRP.)
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
Posts: 4638
Threads: 24
Joined: Aug 2017
Reputation:
207
what fw do you use?
------------------------------
Ctrl+F5
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
(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
Posts: 4638
Threads: 24
Joined: Aug 2017
Reputation:
207
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)
------------------------------
Ctrl+F5
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
Hello
Thanks for the reply.
I'll wait for the update to launch.
Out of curiosity, why is it not working?
a greeting
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
03.06.2020, 21:06
(This post was last modified: 03.06.2020, 21:08 by Erwin van der Zwart.)
Hi,
Because there was a change in the http library that is included in the new FW..
BR,
Erwin
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
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.
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
Hello
Thank you all for answering me.
It is interesting to know.
a greeting
Posts: 184
Threads: 39
Joined: Sep 2015
Reputation:
4
(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
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
Posts: 125
Threads: 16
Joined: May 2020
Reputation:
0
(01.06.2020, 14:29)Daniel. Wrote: It was explained here https://forum.logicmachine.net/showthrea...13#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.
Posts: 215
Threads: 65
Joined: Sep 2015
Reputation:
0
04.08.2020, 09:03
(This post was last modified: 04.08.2020, 09:04 by phongvucba.)
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)
----------------------
Posts: 4638
Threads: 24
Joined: Aug 2017
Reputation:
207
Install new fw and use example above.
------------------------------
Ctrl+F5
Posts: 321
Threads: 72
Joined: Jan 2021
Reputation:
0
(01.06.2020, 14:29)Daniel. Wrote: It was explained here https://forum.logicmachine.net/showthrea...13#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
Best Regards,
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
You are missing this before function telegram(message):
Code: require('ssl.https')
local token = 'token' -- your token
local chat_id = '1234567' -- your chat id
Posts: 321
Threads: 72
Joined: Jan 2021
Reputation:
0
(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')
Best Regards,
Posts: 321
Threads: 72
Joined: Jan 2021
Reputation:
0
Hello
is it possible to send csv file to telegram?
Best Regards,
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
See this example: https://forum.logicmachine.net/showthrea...4#pid17944
Replace name/filename as needed, set ctype to text/csv and finally replace /sendPhoto with /sendDocument in the URL.
|