Logic Machine Forum
Telegram bot API - 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 bot API (/showthread.php?tid=427)



Telegram bot API - Domoticatorino - 18.10.2016

Dear all,
I would like to improve in HomeLynk telegram boat API. Have you any experience about it?
Thanks.


RE: Telegram bot API - Erwin van der Zwart - 18.10.2016

Hi,

I know homeLYnk very well, but i don't understand what you mean with bot or boat..

Can you explain what you want to achieve?

BR,

Erwin van der Zwart


RE: Telegram bot API - Domoticatorino - 18.10.2016

Ciao Erwin,
I mean telegram. Telegram is an instant messages program like pushover but stronger and flexible.
And it is free. I mean telegram bot api https://core.telegram.org/bots/api/.
Thanks.


RE: Telegram bot API - Erwin van der Zwart - 19.10.2016

Hi,

I just checked the Telegram API and it works exactly like the Pushover API, including HTTPS and API Key, the only difference are the used fields.

If you take my Pushover script as sample and rebuild the URL with the correct fields it should work ..

See: http://openrb.com/send-instant-messages-to-ios-and-android-devices-with-lm/ for the Pushover Script

Pushover is also free, and real strong ..

BR,

Erwin van der Zwart


RE: Telegram bot API - Domoticatorino - 19.10.2016

Thank you Erwin. Free Pushover? It's free for 7 days only. Then there's a fee to pay. Have you different information?
Thanks.


RE: Telegram bot API - Erwin van der Zwart - 19.10.2016

Hi,

Yes i see now, it always was a € 4.99 once payment for the app, the app was free last year, now i see they ask again a € 4.99 on time payment after 7 days.

I don't see that as a issue, there are no such things as free services (; and the amount they charge once is almost for free and really acceptable ..

I purchached the app a few years ago, and still working, so good money spend ..

BR,

Erwin van der Zwart


RE: Telegram bot API - Domoticatorino - 27.10.2016

Hi,
I link to a event_based script the following command in order to test the telegram API. I test strin on a url and it works fine hence now I have to replicate on homelynk.
What is wrong?

local res, status = 'https://api.telegram.org/token/sendMessage?chat_id=215120747&text=ciao'

Thanks.


RE: Telegram bot API - admin - 27.10.2016

Code:
require('ssl.https')
body, status, headers, response = ssl.https.request('https://api.telegram.org/token/sendMessage?chat_id=215120747&text=ciao')



RE: Telegram bot API - Domoticatorino - 27.10.2016

Thank you Erwin.
I did it.
Regards.


RE: Telegram bot API - Domoticatorino - 15.11.2016

(27.10.2016, 21:23)Domoticatorino Wrote: Thank you Erwin.
I did it.
Regards.

Hi there,
let me give you the instruction to use Telegram (https://telegram.org/)  as instant message service. Follow these steps:

- install telegram in your phone
- searching on app the BotFather and this easy telegram program can allow you to create your own bot. Here you will find your bot "TOKEN number".
- Share your bot to your user and send a simple message to it.
- After that you can find your CHAT_ID which you can read with a URL request https://api.telegram.org/bot<token>/getUpdates.

Add user script as follow (user.telegram)

require 'ssl.https'

local telegram_url = 'https://api.telegram.org/bot<token_bot>/sendMessage?'

local chat_id = '<chat_id>'

function telegram(message)

  local data_str = 'chat_id=' .. chat_id .. '&text=' .. message..''

     local res, code, headers, status = ssl.https.request(telegram_url, data_str)
end

Add a event-script as follow example:


require("user.telegram")

  message = ('<your own message>')

telegram (message)
end

Have fun!


RE: Telegram bot API - DGrandes - 05.09.2018

Hi,

Im trying to send an image located in the Logic Machine by telegram but it doesn´t work.
Using an url image from internet work perfectly

My code is:

local telegram_url_sendphototo = "https://api.telegram.org/bot<token>/sendPhoto?"

function SendPhotoTo (photo)
local chat_id ='353575234245'
local data_str = 'chat_id='..chat_id..'&photo='..photo..""
local res, code, headers, status = ssl.https.request(telegram_url_sendphototo,data_str)
log (res,code,headers,status)
end

--This code works perfectly
SendPhotoTo("http://www.imagen.com.mx/assets/img/imagen_share.png")

--But this code donesn´t work. I can open in browser but it is not sended by telegram
SendPhotoTo("http://<LogicMachineIP:port>/scada/resources/img/imagen_share.png")

Also I´m trying to send the image that I get from the IPCamera, please see the following link https://forum.logicmachine.net/showthread.php?tid=1061&pid=9364#pid9364

SendPhotoTo(image)

But it doesnt work

Can anyone help me please?
Thanks


RE: Telegram bot API - baggins - 09.02.2020

I have used Domoticatorino's Telegram script for a couple of years without problem, but recently it started to fail.

When I call the function I have the following result:
Code:
test_telegram 09.02.2020 23:41:30
* string: status: nil
test_telegram 09.02.2020 23:41:30
* string: headers: nil
test_telegram 09.02.2020 23:41:30
* string: code: revcd alert fatal error
test_telegram 09.02.2020 23:41:30
* string: res: nil


I can run the same script on my desktop machine and there it runs without errors...
Any idea what may be going on?

Update: I have tested this on another LM4 where the function is also in use and there the script does not work anymore either.
I also moved the code of the function to the test script, so I don't need a library call, with the same result.
Firmware: 20160714


RE: Telegram bot API - admin - 10.02.2020

You are using outdated firmware where SSLv3 is used by default. It is disabled on most servers and you have to use TLSv1.2 or newer. Try using this function to make HTTPS GET request:
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



RE: Telegram bot API - baggins - 10.02.2020

(10.02.2020, 06:40)admin Wrote: You are using outdated firmware where SSLv3 is used by default. It is disabled on most servers and you have to use TLSv1.2 or newer. Try using this function to make HTTPS GET request:
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

At the moment I cannot upgrade because of a third party library...

I tried the following, but got the same error:
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 telegram_url = 'https://api.telegram.org/mybot/sendMessage?'

local chat_id = '123456789'

message = "test"
data_str = 'chat_id=' .. chat_id .. '&text=' .. message..''
stat, code, hdrs = request(telegram_url, data_str)
log("code: " ..tostring(code))
log("headers: " ..tostring(hdrs))
log("status: " ..tostring(stat))



RE: Telegram bot API - admin - 10.02.2020

You are trying to do a POST request, try GET like this:
Code:
stat, code, hdrs = request(telegram_url .. data_str)

If this still does not work then the only solution is to do firmware upgrade to 2019 RC1.


RE: Telegram bot API - baggins - 10.02.2020

(10.02.2020, 08:22)admin Wrote: You are trying to do a POST request, try GET like this:
Code:
stat, code, hdrs = request(telegram_url .. data_str)

If this still does not work then the only solution is to do firmware upgrade to 2019 RC1.

It doesn't work Sad

I will have to figure out how to upgrade...
Thanks for your help.


RE: Telegram bot API - Dario - 19.03.2020

(10.02.2020, 08:22)admin Wrote: You are trying to do a POST request, try GET like this:
Code:
stat, code, hdrs = request(telegram_url .. data_str)

If this still does not work then the only solution is to do firmware upgrade to 2019 RC1.

Hi!

I used in my message "\n" to go new line.
Now I receive only first line:

Code:
if grp.getvalue('9/2/7')== true then txtstate='On' else txtstate='Off' end
      txtmessage = txtmessage .. ('Primario: ' .. txtstate .. '\n')
   if grp.getvalue('9/2/1')== true then txtstate='On' else txtstate='Off' end
      txtmessage = txtmessage .. ('Caldaia: ' .. txtstate .. '\n')

local stat, code, hdrs = request(telegram_url .. txtmessage)


Message on log is ok, message on telegram is:

"Primario: On" not else.


RE: Telegram bot API - admin - 19.03.2020

You need to escape your text sequence:
Code:
txtmessage = require('socket.url').escape(txtmessage)
local stat, code, hdrs = request(telegram_url .. txtmessage)



RE: Telegram bot API - Dario - 20.03.2020

Perfect!


RE: Telegram bot API - JRP - 12.05.2020

Hello
I am interested in using this telegram script but among the solutions from the beginning, the new ones and the corrections I am not clear.
It would be possible to compile a complete script that currently works and with the appropriate steps.
Thank you.
a greeting
P.S. I have a Wiser for knx with the latest firmware and latest hardware.