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)

Log in to post a comment.