First Native Telegram Bot for codesys

Hi All

We are using Telegram bots since a long time for remote monitoring our standalone machines.

Finally i cleaned up the code so far, that i can share it with the public.

feel free to use it, modify and do whatever you want with it. In case you do improvements, please post them here so we can keep it up to date.

... maybe at some point i will also post it on github.

First, install the library attached here. I assume that you already have created your bot with botFather and are familiar about ChatIDs. otherwise, google is your friend ;-)

it needs the IIOT Library from codesys, don't forget also to buy a license to keep it working longer than 1 hour.

example Telegram Sending bot:

PROGRAM TelegramSender
VAR CONSTANT
    token : STRING := 'INSERT YOUR TOKEN HERE';
END_VAR

VAR
    telegramsnd : MessageSender;
    sMessage : STRING;
    iChatID : DINT;
    xSendMessage : BOOL;
END_VAR

---------

telegramsnd(sToken:=token, sMessage:=sMessage, iChatID:=iChatID,xSendMessage:=xSendMessage);

IF telegramsnd.xBusy OR telegramsnd.xDone OR telegramsnd.xError THEN
    xSendMessage:=FALSE;
END_IF

Example receiving bot:

PROGRAM TelegramReceiver
VAR CONSTANT
    token : STRING := 'INSERT_YOUR_TOKEN_HERE';
    tMessageRefresh : TIME := T#10S;
END_VAR
VAR
    telegramrcv : MessageReceiver;
    breceive : BOOL;


    sMessage : STRING;
    iChatID :DINT;
    uiMessageID: UDINT;

    tonReadMessage : TON;   
END_VAR

-----------------

tonReadMessage(in:=NOT bReceive AND NOT telegramrcv.xBusy, pt:= tMessageRefresh);
IF tonReadMessage.Q THEN
    bReceive:=TRUE;
END_IF

telegramrcv(Token:=token, xExecute:=breceive);

IF telegramrcv.xBusy OR telegramrcv.xError THEN
    breceive:=FALSE;
END_IF

IF telegramrcv.xNewMessage THEN
    sMessage:=telegramrcv.sMessage;
    iChatID:=telegramrcv.iChatID;
    uiMessageID:=telegramrcv.uiMessageID;
END_IF
Posted by kevinl 2022-07-16
Attachments:
Telegram_Bot.library (126560 bytes)
  • albertocamina - 2022-11-12

    Thanks for the library Kevinl. It works beautifuly. Im having an issue when the text to send contains a "line break" because I reveive an error. If the text don't have any line break message is sent correctly. I tryed with '$L', '$l','$N', '$n', \n, etc, unsuccessfully. Any ideas? All the best!

     
  • kevinl - 2022-11-13

    hi albertocamina
    funny that it is not working with $N, this may be a bug of my library i should check...

    anyway, i recently did a bot with german umlaute (ÄÖÜ) and used unicode characters. that works like this:
    Ü = Unicode C3BC = %C3%BC, the % sign in a ASCII is $25 so Ü is $25C3$25BC

    now the answer to your question, you can use unicode newline (000A) that wil look like this: $2500$250A
    so the string
    'albertocamina$2500$250Aprefers messages on 2 lines'

    btw you can also go with emojis, for example
    $25F0$259F$2598$2583 = 😃

    i hope that helps for the moment.

     

    Last edit: kevinl 2022-11-13
  • albertocamina - 2022-11-28

    Many thanks! Tested and operating perfectly!

     
  • albertocamina - 2022-11-28

    Many thanks! Tested and operating perfectly!

     
  • kevinl - 2022-11-28

    great, glad it works

     
  • holger - 2023-03-02

    Gute Arbeit

     
  • holger - 2023-03-13

    Well done. works very well.
    do you have a solution for buttons?
    (reply_markup...)

     
  • kevinl - 2023-03-13

    not yet, i am working on a update with much better performance and also less data usage wich i will propably release in summer...

    also i am currently pretty busy, let me know how important the buttons are for you and i'll consider adding it soon in a update.

    if you like to add the buttons yourself, please make sure to post your code here. i can also move the project to github if there is demand for helping in development.

    kind regards

     
  • sajoo - 2023-08-28

    Feel free to share your code for public use. Using Telegram bots for remote monitoring of standalone devices is really practical. It's great that you cleaned up your code and are now sharing it with others.

    i saw another cods to create telegram bot on https://eshloon.ir

     

    Last edit: sajoo 2024-02-11

Log in to post a comment.