"For developers", "Server scripts", "Functions description", "Integrations", "Telegram", "mIntegrationTelegramGetMyChatUINByTelegramID",
Function to obtain MyChat user UIN by his Telegram ID.
Usually this function is used when binding with the OnRequestByREST event to send a reply from Telegram to MyChat.
function mIntegrationTelegramGetMyChatUINByTelegramID(sID: string): integer;
Parameter |
Type |
Value |
---|---|---|
sID |
string |
Telegram user ID. |
-1 |
such Telegram ID is not registered in the MyChat Server database; |
-2 |
ID found, but MyChat user is not linked to this ID or is not activated yet; |
>0 |
all ok, MyChat user UIN which is linked with the specified Telegram user. |
Processing OnRequestByRest event; the "Telegram" script processes the incoming message, and if it is not the "/start" bot command, the script decides that this is the answer and it must be sent from Telegram to the specified MyChat user.
function OnRequestByREST(sBody, sParams, sHeaders, sURL, sIPv4, sIPv6: string; iType: integer): string;
var
sFirstName, sLastName, sNickName, sTelegramID, sText, sData: string;
iResult, x, iUIN, iUINFrom: integer;
begin
sFirstName := '';
sLastName := '';
sNickName := '';
sTelegramID := '';
sText := '';
// get request data
JSONGetString(sBody, 'message.from.id', sTelegramID);
JSONGetString(sBody, 'message.from.first_name', sFirstName);
JSONGetString(sBody, 'message.from.last_name', sLastName);
JSONGetString(sBody, 'message.from.username', sNickName);
JSONGetString(sBody, 'message.text', sText);
// if user is connecting to the Telegram bot for the first time
if LowerCase(sText) = '/start' then mIntegrationTelegramAddUser(sTelegramID, sFirstName, sLastName, sNickName)
else begin // maybe user send reply message to the bot
iResult := JSONGetString(sBody, 'message.reply_to_message.entities[0].url', sData);
if iResult = 0 then begin
// get receiver's UIN
x := pos('?uin=', sData) + 5;
iUIN := StrToIntDef(copy(sData, x, pos('&', sData) - x), -1);
// get sender's UIN by user Telegram ID
iUINFrom := mIntegrationTelegramGetMyChatUINByTelegramID(sTelegramID);
// if sender and receiver are exists
if (iUIN <> -1) and (iUINFrom > 0) then begin
// multi-line text
sData := '[Telegram] ' + ReplaceString(sText, #10, CRLF, true, false);
// send private message into MyChat
mSendPrivateMessage(iUINFrom, iUIN, sData, 1, true);
end;
end;
end;
result := '{}';
end;
begin
end.
Sending a reply from Telegram:
Receiving a message in MyChat: