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
#1
Good morning,

there is the possibility of transmitting events with this application (e.g. tripping protections, exceeded threshold, ..)
Reply
#2
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
Reply
#3
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
Reply
#4
what fw do you use?
------------------------------
Ctrl+F5
Reply
#5
(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
Reply
#6
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
Reply
#7
Hello
Thanks for the reply.

I'll wait for the update to launch.

Out of curiosity, why is it not working?
a greeting
Reply
#8
Hi,

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

BR,

Erwin
Reply
#9
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.
Reply
#10
Hello
Thank you all for answering me.
It is interesting to know.
a greeting
Reply
#11
(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
Reply
#12
20160714 is too old
Reply
#13
(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.
Reply
#14
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)
----------------------
Reply
#15
Install new fw and use example above.
------------------------------
Ctrl+F5
Reply
#16
(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,
Reply
#17
You are missing this before function telegram(message):
Code:
require('ssl.https')

local token = 'token' -- your token
local chat_id = '1234567' -- your chat id
Reply
#18
(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
Best Regards,
Reply
#19
Hello
is it possible to send csv file to telegram?
Best Regards,
Reply
#20
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.
Reply


Forum Jump: