From 7c59f1d17fd367f17ad604d0ad7e7303458fe2d8 Mon Sep 17 00:00:00 2001 From: FreiherrKenny <64729610+FreiherrKenny@users.noreply.github.com> Date: Tue, 8 Sep 2020 07:23:17 +0200 Subject: [PATCH] working solution for dropped vpn connections Added _Telegram_HttpCrashHandler() to fetch errors related to COM errors like these: err.number is: 80020009 err.lastdllerror is: 0 err.scriptline is: 1120 err.source is: WinHttp.WinHttpRequest err.helpfile is: err.helpcontext is: 0 --- src/Telegram.au3 | 336 +++++++++++++++++++++++++---------------------- 1 file changed, 179 insertions(+), 157 deletions(-) diff --git a/src/Telegram.au3 b/src/Telegram.au3 index 5fc20e1..371600f 100755 --- a/src/Telegram.au3 +++ b/src/Telegram.au3 @@ -24,38 +24,42 @@ Global $URL = "https://api.telegram.org/bot" Global $OFFSET = 0 ;@CONST -Const $BOT_CRLF = __UrlEncode(@CRLF) +Const $BOT_CRLF = _Telegram_UrlEncode(@CRLF) Const $INVALID_TOKEN_ERROR = 1 Const $FILE_NOT_DOWNLOADED = 2 Const $OFFSET_GRATER_THAN_TOTAL = 3 Const $INVALID_JSON_RESPONSE = 4 +;@ObjEvents +$oMyError = ObjEvent("AutoIt.Error","_Telegram_HttpCrashHandler") + #Region "@ENDPOINT FUNCTIONS" #cs =============================================================================== - Function Name..: _GetUpdates + Function Name..: _TelegramGetUpdates Description....: Used by _Polling() to get new messages Parameter(s)...: None Return Value(s): Return string with information encoded in JSON format #ce =============================================================================== -Func _GetUpdates() - Return __HttpGet($URL & "/getUpdates?offset=" & $OFFSET) -EndFunc ;==> _GetUpdates +Func _TelegramGetUpdates() + Return _Telegram_HttpGet($URL & "/getUpdates?offset=" & $OFFSET) +EndFunc ;==> _TelegramGetUpdates #cs =============================================================================== - Function Name..: _GetMe - Description....: Get information about the bot (ID,Username,Name) + Function Name..: _TelegramGetMe + Description....: Get information about the bot (ID,Username,First name,Last name) Parameter(s)...: None Return Value(s): Return an array with information #ce =============================================================================== -Func _GetMe() - Local $json = Json_Decode(__HttpGet($URL & "/getMe")) +Func _TelegramGetMe() + Local $json = Json_Decode(_Telegram_HttpGet($URL & "/getMe")) If Not (Json_IsObject($json)) Then Return SetError($INVALID_JSON_RESPONSE,0,False) ;Check if json is valid - Local $data[3] = [Json_Get($json,'[result][id]'), _ + Local $data[4] = [Json_Get($json,'[result][id]'), _ Json_Get($json,'[result][username]'), _ - Json_Get($json,'[result][first_name]')] + Json_Get($json,'[result][first_name]'), _ + Json_Get($json,'[result][last_name]')] Return $data -EndFunc ;==>_GetMe +EndFunc ;==>_TelegramGetMe #cs =============================================================================== Function Name..: _SendMsg @@ -69,7 +73,7 @@ EndFunc ;==>_GetMe $DisableNotification (optional): Sends the message silently. User will receive a notification with no sound Return Value(s): Return the Message ID if no error encountered, False otherwise #ce =============================================================================== -Func _SendMsg($ChatID,$Text,$ParseMode = Default,$ReplyMarkup = Default,$ReplyToMessage = '',$DisableWebPreview = False,$DisableNotification = False) +Func _TelegramSendMsg($ChatID,$Text,$ParseMode = Default,$ReplyMarkup = Default,$ReplyToMessage = '',$DisableWebPreview = False,$DisableNotification = False) Local $Query = $URL & "/sendMessage?chat_id=" & $ChatID & "&text=" & $Text If StringLower($ParseMode) = "markdown" Then $Query &= "&parse_mode=markdown" If StringLower($ParseMode) = "html" Then $Query &= "&parse_mode=html" @@ -77,7 +81,7 @@ Func _SendMsg($ChatID,$Text,$ParseMode = Default,$ReplyMarkup = Default,$ReplyTo If $DisableNotification = True Then $Query &= "&disable_notification=True" If $ReplyToMessage <> '' Then $Query &= "&reply_to_message_id=" & $ReplyToMessage If $ReplyMarkup <> Default Then $Query &= "&reply_markup=" & $ReplyMarkup - Local $Json = Json_Decode(__HttpPost($Query)) + Local $Json = Json_Decode(_Telegram_HttpPost($Query)) If Not (Json_IsObject($Json)) Then Return SetError($INVALID_JSON_RESPONSE,0,False) ;Check if json is valid If Not (Json_Get($Json,'[ok]') = 'true') Then Return SetError(2,0,False) ;Return false if send message faild Return Json_Get($Json,'[result][message_id]') ;Return message_id instead @@ -92,10 +96,10 @@ EndFunc ;==> _SendMsg $DisableNotification (optional): Sends the message silently. User will receive a notification with no sound Return Value(s): Return the new Message ID if no error encountered, False otherwise #ce =============================================================================== -Func _ForwardMessage($ChatID,$OriginalChatID,$MsgID,$DisableNotification = False) +Func _TelegramForwardMessage($ChatID,$OriginalChatID,$MsgID,$DisableNotification = False) Local $Query = $URL & "/forwardMessage?chat_id=" & $ChatID & "&from_chat_id=" & $OriginalChatID & "&message_id=" & $MsgID If $DisableNotification Then $Query &= "&disable_notification=True" - Local $Json = Json_Decode(__HttpPost($Query)) + Local $Json = Json_Decode(_Telegram_HttpPost($Query)) If Not (Json_IsObject($Json)) Then Return SetError($INVALID_JSON_RESPONSE,0,False) ;Check if json is valid If Not (Json_Get($Json,'[ok]') = 'true') Then Return SetError(2,0,False) Return Json_Get($Json,'[result][message_id]') ;Return message_id instead @@ -112,7 +116,7 @@ EndFunc ;==> _ForwardMessage $DisableNotification (optional): Sends the message silently. User will receive a notification with no sound Return Value(s): Return the File ID of the photo as string #ce =============================================================================== -Func _SendPhoto($ChatID,$Photo,$Caption = '',$ReplyMarkup = Default,$ReplyToMessage = '',$DisableNotification = False) +Func _TelegramSendPhoto($ChatID,$Photo,$Caption = '',$ReplyMarkup = Default,$ReplyToMessage = '',$DisableNotification = False) Local $Query = $URL & '/sendPhoto' Local $hOpen = _WinHttpOpen() Local $Form = '