GithubHelp home page GithubHelp logo

xlinkout / telegram-udf-autoit Goto Github PK

View Code? Open in Web Editor NEW
53.0 15.0 15.0 3.07 MB

πŸš€ Telegram Bot UDFs for AutoIt.

License: MIT License

AutoIt 100.00%
telegram-udf autoit bot telegram-bot udfs

telegram-udf-autoit's Introduction

Telegram Bot UDFs for AutoIt

A collection of user defined functions to seamlessly control your Telegram Bot with AutoIt. It support most of the Telegram Bot API and offer a whole set of useful features to interact with them.

Note

This library is listed in the official AutoIt Script Wiki!. Also, refer to the original forum topic for more details.

Important

I've rewritten the library code from scratch after years of inactivity. The aim is to update, optimize it, fix all reported issues accumulated over the years, and support the latest Telegram features. It's still in work in progress and, obviously, there are breaking changes. You can find the development in the dev branch; I appreciate any kind of contribution. Also, in the release section, you can find the previous version for backward compatibility. Thank you for the support!

How to use

The library itself is in the src/Telegram.au3 file. It need all the dependencies in the src/include folder: WinHttp, JSON.

First, include the library in your script with #include "path/to/Telegram.au3". Then, you can initialize the bot with the _Telegram_Init function: it take the bot token as first parameter (given to you by BotFather), and a boolean that validate the token as second parameter. I recommend to always validate your token, so the script fail immediately if it is invalid. The function return True if everything is ok, or False otherwise, and set @error and @extended accordingly.

After this initialization step, you can use all the other functions. Refer to the wiki for more examples.

What functions return

The main difference from previous version of this library is that every Telegram API call return the response object almost as-is; it check the response status and return the result inner object to the caller. If any error occurs during the HTTP request, the function return Null and set @error and @extended flags.

That said, when you call any Telegram-related functions, expect in return an object as described in the Telegram API documentation. Use the JSON library to retrieve the information you need.

Read incoming messages

If you want to read incoming messages, you can put the bot in polling mode: the script will poll for incoming messages and return them. This state is blocking, therefore your script will wait here until a message is received or it exit. For example, to create a simple echo bot that receive a message and send it back, you can do the following:

; Retrieve an array of messages from Telegram
$aMessages = _Telegram_Polling()
; For each message, send it back
For $i = 0 To UBound($aMessages) - 1
    $sChatId = Json_Get($aMessages[$i], "[message][chat][id]")
    $sMessage = Json_Get($aMessages[$i], "[message][text]")
    _Telegram_SendMessage($sChatId, $sMessage)
Next

License

The license for this project has been updated from GPL to MIT. This means that you are now free to use this work in your projects, with the condition that you acknowledge and cite this library within your own work. Thank you for your support and cooperation.

telegram-udf-autoit's People

Contributors

cfluegel avatar xlinkout avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

telegram-udf-autoit's Issues

I get an error when starting the UDF

I have a problem with _Polling, I tried to fix this error myself, but it didn't work, although when I downloaded your UDF everything worked, but after about 15 minutes, when I wanted to run the Autoit script again, I started getting an error

$OFFSET = $msgData[0] + 1
$OFFSET = $msgData^ ERROR

I tried to take the code that you show for example

While 1 ;Create a While that restart Polling
	$msgData = _Polling() ;_Polling function return an array with information about a message
	_SendMsg($msgData[2],$msgData[5]) ;Send a message to the same user with the same text
WEnd

but still this error comes out

"C:\Users\staso\Desktop\New folder (7)\src\Telegram.au3" (794) : ==> Subscript used on non-accessible variable.:
$OFFSET = $msgData[0] + 1
$OFFSET = $msgData^ ERROR
>Exit code: 1    Time: 2.255

Maybe I made a error, here is my code:

#include <src/Telegram.au3>

_InitBot('1907...:AAEjer1ifIkv_...')

_SendMsg("...", "Hello")

While 1
   $msgData = _Polling()
   _SendMsg($msgData[2], $msgData[5])
WEnd

The bot token is correctly specified in the code itself, since _SendMsg works

Polling stops when internet connection is lost

Hey, I have to thank you very much for this UDF. It is truly excellent. I am using it to automate thr communication with remote computers.

I currently have one issue though. I run the polling script when user logs in and it should keep polling as long as the user is logged in. I noticed however that once an internet connection is lost, even for a few minutes, the script returns 0x1 code and stops.

I can understand it stops but is there any way for polling to simply skip checking for new messages while a computer is not accessible and start polling again when it is online? I mean some sort of check for the script not to end?

I found this solution https://eddiejackson.net/wp/?p=7530 and I tested another one so far. Well, they don't really seem to do the job.

Error

No "EndFunc"
Func _CreateInlineKeyboard(ByRef $Keyboard)
Local $jsonKeyboard = '{"inline_keyboard":[['
Return $jsonKeyboard
EndFunc

#include <String.au3>
740 $string[$i] = "%"&_StringToHex($string[$i])

Support for Callbacks

If you try to use reply_markup with "inline_keyboard" it will give you an error in the _JSONDecode function.

How could I use inline_keyboard and get callbacks without problems?

Getting error when use Func __Polling() without internet connection

Getting error when use Func __Polling() without internet connection.

telegram.au3" (1119) : ==> The requested action with this object has failed.:
$oHTTP.Send()
$oHTTP^ ERROR

Exit code: 1

====
$oHTTP.Send() work with Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")

ran test sample failed for sending image/audio/video/photo function

I downloaded telegram UDF files from github. I ran the sample code and most telegram functions failed.
Only the "send message, location, contact" working well , the others such as send audio/image/video.... all failed.
I don't know what's wrong. Could some give me suggestion?
note: I used autoit v3.3.14.5

Incorrect text decoding for channel messages

No text in $msgData[5] when an admin sent a message in a channel while looping _Pollling.
_getUpdates recognizes the text of the message.

_getUpdates output message:
_getUpdates Output

$msgData from _Polling:
$msgData from _Pollig Output

I looked into the Telegram UDF and checked that the array index are correct.I saw that behind the line for the text it sais ";[5] = Text (eventually)" Does that "eventually" has something to do with my issue?

Thanks!

i got error messege when use func polling

telegram-udf-autoit-master\src\Telegram.au3" (794) : ==> Subscript used on non-accessible variable.:
$OFFSET = $msgData[0] + 1
$OFFSET = $msgData^ ERROR
can someone help

thank for reply

line-break doesn't work

Hi, when I try to use @CRLF it doesn't work when I send it to telegram, I have to use %0D%0A or only %0A directly in the String to generate the line break.

You could create a constant or encode the text string to be compatible with URL


https://www.autoitscript.com/forum/topic/144032-url-encoding/

#include <String.au3>
#include <Array.au3>

Func _urlencode($string)
    $string = StringSplit($string, "")
    For $i=1 To $string[0]
        If AscW($string[$i]) < 48 Or AscW($string[$i]) > 122 Then
            $string[$i] = "%"&_StringToHex($string[$i])
        EndIf
    Next
    $string = _ArrayToString($string, "", 1)
    Return $string
EndFunc

ConsoleWrite(_urlencode("Test"&@CRLF&"Test"))

Result:

Test%0D%0ATest

Syntax error Telegram.au3

Downloaded it and try to run some test scripts always get the following syntax error

"C:\Users\Nils\Desktop\telegram-udf-autoit-master\Telegram.au3"(619,46) : error: syntax error
"name:photo" , $Path,

"C:\Users\Nils\Desktop\telegram-udf-autoit-master\example\CustomKeyboard.au3"(39,99) : error: syntax error
ConsoleWrite("Sending contact... "& _SendContact($ChatID,"0123456789","Josh","Doe") & "... Done!" @CRLF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Nils\Desktop\telegram-udf-autoit-master\example\CustomKeyboard.au3 - 2 error(s), 0 warning(s)
!>17:18:27 AU3Check ended. Press F4 to jump to next error.rc:2
+>17:18:27 AutoIt3Wrapper Finished.
>Exit code: 2    Time: 0.5949

Error when polling: Subscript used on non-accessible variable

This is the error message:

Line 794 [in](https://www.autoitscript.com/autoit3/docs/keywords.htm) Telegram.au3
$OFFSET = $msgData[0] + 1
$OFFSET = $msgData^ERROR
Error: Subscript used on non-accessible variable

It was solved by @jos on the AutoIt forum, please check complete thread here:
Subscript used on non-accessible variable
https://www.autoitscript.com/forum/topic/186381-telegram-bot-udf/page/6/#comments

Suggested Changes (in Telegram.au3):
Try replacing func __MsgDecode() with this version first as that will likely solve it for you:

Func __MsgDecode($Update)
    Local $json = Json_Decode($Update)

    ;@PRIVATE CHAT MESSAGE

    If (Json_Get($json, '[result][0][message][chat][type]') = 'private') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _
                Json_Get($json, '[result][0][message][message_id]'), _
                Json_Get($json, '[result][0][message][from][id]'), _
                Json_Get($json, '[result][0][message][from][username]'), _
                Json_Get($json, '[result][0][message][from][first_name]') _
                ]

        If (Json_Get($json, '[result][0][message][text]')) Then $msgData[5] = Json_Get($json, '[result][0][message][text]')

        ; TODO: Media recognition

        Return $msgData

        ;@GROUP CHAT MESSAGE (Inlude left/new member events)
    ElseIf (Json_Get($json, '[result][0][message][chat][type]') = 'group') Or (Json_Get($json, '[result][0][message][chat][type]') = 'supergroup') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][message][message_id]'), _ ;[1] = Message ID
                Json_Get($json, '[result][0][message][from][id]'), _ ;[2] = User ID
                Json_Get($json, '[result][0][message][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][message][from][first_name]'), _ ;[4] = Firstname
                Json_Get($json, '[result][0][message][chat][id]'), _ ;[5] = Group ID
                Json_Get($json, '[result][0][message][chat][title]') _ ;[6] = Group Name
                ]

        If (Json_Get($json, '[result][0][message][left_chat_member]')) Then
            $msgData[7] = 'left' ;[7] = Event
            $msgData[8] = Json_Get($json, '[result][0][message][from][id]') ;[8] = Left member ID
            $msgData[9] = Json_Get($json, '[result][0][message][from][username]') ;[9] = Left member Username
            $msgData[10] = Json_Get($json, '[result][0][message][from][first_name]') ;[10] = Left member Firstname
        ElseIf (Json_Get($json, '[result][0][message][new_chat_member]')) Then
            $msgData[7] = 'new' ;[7] = Event
            $msgData[8] = Json_Get($json, '[result][0][message][from][id]') ;[8] = New member ID
            $msgData[9] = Json_Get($json, '[result][0][message][from][username]') ;[9] = New member Username
            $msgData[10] = Json_Get($json, '[result][0][message][from][first_name]') ;[10] = New member Firstname
        Else
            $msgData[7] = Json_Get($json, '[result][0][message][text]') ;[7] = Text
        EndIf

        Return $msgData

        ;@EDITED PRIVATE CHAT MESSAGE
    ElseIf (Json_Get($json, '[result][0][edited_message][chat][type]') = 'private') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][edited_message][message_id]'), _ ;[1] = Message ID
                Json_Get($json, '[result][0][edited_message][from][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][edited_message][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][edited_message][from][first_name]') _ ;[4] = Firstname
                ]

        If (Json_Get($json, '[result][0][edited_message][text]')) Then $msgData[5] = Json_Get($json, '[result][0][edited_message][text]') ;[5] = Text (eventually)

        ;Insert media recognition here

        Return $msgData

        ;@EDITED GROUP CHAT MESSAGE
    ElseIf (Json_Get($json, '[result][0][edited_message][chat][type]') = 'group') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][edited_message][message_id]'), _ ;[1] = Message ID
                Json_Get($json, '[result][0][edited_message][from][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][edited_message][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][edited_message][from][first_name]') _ ;[4] = Firstname
                ]

        If (Json_Get($json, '[result][0][edited_message][text]')) Then $msgData[5] = Json_Get($json, '[result][0][edited_message][text]') ;[5] = Text (eventually)

        ;Insert media recognition here

        Return $msgData

        ;@CALLBACK QUERY
    ElseIf (Json_Get($json, '[result][0][callback_query][id]') <> '') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][callback_query][id]'), _ ;[1] = Callback ID
                Json_Get($json, '[result][0][callback_query][from][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][callback_query][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][callback_query][from][first_name]'), _ ;[4] = Firstname
                Json_Get($json, '[result][0][callback_query][data]') _ ;[5] = Callback Data
                ]

        Return $msgData

        ;@INLINE QUERY
    ElseIf (Json_Get($json, '[result][0][inline_query][id]') <> '') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][inline_query][id]'), _ ;[1] = Inline Query ID
                Json_Get($json, '[result][0][inline_query][from][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][inline_query][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][inline_query][from][first_name]'), _ ;[4] = Firstname
                Json_Get($json, '[result][0][inline_query][query]') _ ;[5] = Inline Query Data
                ]

        Return $msgData

        ;@CHANNEL MESSAGE (Where bot is admin)
        ; Sample JSON:
        #comments-start
            {"ok":true,"result":[{
                "update_id":<int>,
                "channel_post":{
                    "message_id":<int>,
                    "chat":{
                    "id":<int>,
                    "title":"<string>",
                    "type":"channel"},
                    "date":<int>,
                    "text":"<string>"
            }}]}
        #comments-end
    ElseIf (Json_Get($json, '[result][0][channel_post][message_id]') <> '') Then
        Local $msgData[5] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][channel_post][message_id]'), _ ;[1] = Message ID
                Json_Get($json, '[result][0][channel_post][chat][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][channel_post][chat][title]') _ ;[3] = Firstname
                ]

        If (Json_Get($json, '[result][0][channel_post][text]')) Then
            $msgData[4] = Json_Get($json, '[result][0][channel_post][text]') ;[4] = Text (eventually)
        EndIf

        Return $msgData

        ;@EDITED CHANNEL CHAT MESSAGE
    ElseIf (Json_Get($json, '[result][0][edited_channel_post][chat][type]') = 'channel') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][edited_channel_post][message_id]'), _ ;[1] = Message ID
                Json_Get($json, '[result][0][edited_channel_post][from][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][edited_channel_post][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][edited_channel_post][from][first_name]') _ ;[4] = Firstname
                ]

        If (Json_Get($json, '[result][0][edited_channel_post][text]')) Then $msgData[5] = Json_Get($json, '[result][0][edited_channel_post][text]') ;[5] = Text (eventually)

        ;Insert media recognition here

        Return $msgData
        ;@EDITED CHANNEL CHAT MESSAGE
    ElseIf (Json_Get($json, '[result][0][my_chat_member][chat][type]') = 'group') Then
        Local $msgData[10] = [ _
                Json_Get($json, '[result][0][update_id]'), _ ;[0] = Offset
                Json_Get($json, '[result][0][my_chat_member][message_id]'), _ ;[1] = Message ID
                Json_Get($json, '[result][0][my_chat_member][from][id]'), _ ;[2] = Chat ID
                Json_Get($json, '[result][0][my_chat_member][from][username]'), _ ;[3] = Username
                Json_Get($json, '[result][0][my_chat_member][from][first_name]') _ ;[4] = Firstname
                ]
        $msgData[5] = ""
        If (Json_Get($json, '[result][0][my_chat_member][text]')) Then $msgData[5] = Json_Get($json, '[result][0][my_chat_member][text]') ;[5] = Text (eventually)

        ;Insert media recognition here

        Return $msgData

    EndIf

EndFunc   ;==>__MsgDecode

Please do look into this and update the UDF accordingly. Thanks.

username or first_name

First of all awesome work, saved me some time
Telegram UDF.zip

I had to change the code to make it work for me, in the _JSONDecode($) function when looking for the name(for me at least) the "username" field wasn't there and it was named "first_name" instead, you check if the username is blank after the first attempt but I had to change: "$dataArray[3]" which is the text for "$dataArray[1]", also it stored the value in $dataArray[3], I changed to $dataArray[1]. I suppose the whole thing may be some api change or just me trying it on a private chat, but I attached my file for you to see, just changed those 2 lines and it seems to work well.

Exit used instead of SetError

In the _InitBot() function, you call Exit instead of SetError. Is this the wanted behaviour?
It's really confusing when the script exits without any message, except a ConsoleWrite.
Especially in a compiled script you don't know why the script ends.

btw: nice work πŸ‘

Fix for _answerCallbackQuery()

Broken right now.
Change the following lines:
Line 517 to: Func _answerCallbackQuery($CallbackID,$Text = '',$cusURL = '',$ShowAlert = False,$CacheTime = '')
Line 518 to: Local $Query = $URL & "/answerCallbackQuery?callback_query_id=" & $CallbackID
Line 520 to: If $cusURL <> '' Then $Query &= "&url=" & $cusURL

_SendMsg with Url Encode

hi! I love this project

Wanted to propose an improvement in the _SendMsg function
If I want to make a line break I have to put %0A that equals \n

Could you integrate such a function?
Since I work in the Spanish language and we have accents and characters that must be encoded
Ex. Γ‘ Γ© Γ­ Γ³ ΓΊ Γ± -> %C3%A1 %C3%A9 %C3%AD %C3%B3 %C3%BA %C3%B1`

have a nice day! ✌️

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.