
; -------------------------------Telegram-------------------------------
; API documentation
; https://core.telegram.org/bots/api#sendmessage
PostToTelegram:
CurrentSite := "Telegram"

; SaveOrPostProgress(Message:="Posting through API",PostType:="Tooltip,ErrorLoggingTextFile")
Message = Posting Through API
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


if(TelegramBotToken = "" Or TelegramBotChatID =""){
	Message = TelegramBotToken or TelegramBotChatID is missing from %SettingsIniFilepath%. `nPlease input them and rerun the script.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	; Msgbox, 4096, Error!, TelegramBotToken or TelegramBotChatID is missing from %SettingsIniFilepath%. `nPlease input them and rerun the script.
	Return
}


LineBreakChar = `%0A ; Used for API


TelegramTitle := ASCIISTRReplace(PostTitle)
TelegramBody := ASCIISTRReplace(PostBody)


/*	TelegramTitle := PostTitle
	TelegramBody := PostBody

	*/

	; Msgbox % "TelegramBody: " TelegramBody

	if(PostBody = ""){
		if(BoldTitleWAPI)
		Message := "*" . TelegramTitle . "*"
		else, 
		Message := TelegramTitle
	}
	else, { ; if postbody is not blank
		; MsgBox, we in post body
		if(BoldTitleWAPI){
			if(NewLineBetweenTitle)
			Message := "*" . TelegramTitle . "*" . "`n`n" . TelegramBody
			else, 
			Message := "*" . TelegramTitle . "*" . "`n" . TelegramBody
		}
		else, { ; NOT Bold Title
			if(NewLineBetweenTitle)
			Message := TelegramTitle . "`n`n" . TelegramBody
			else, 
			Message := TelegramTitle . "`n" . TelegramBody

		}
	}

	; Msgbox % "Message: " Message
	; REMOVE
	; Message := PostTitle . "`n" . PostBody

	; Msgbox % "TelegramTitle: " TelegramTitle
	; Msgbox % "TelegramBody: " TelegramBody

	; Msgbox % "Message: " Message


	if(ImageAttachmentFilepath != "" AND StrLen(Message) > 1020){ ; utilize function that converts image to MIME/multi form 
		; Telegram API character limit of 4096 for a basic message.
		; @todo: add functionality to split messages longer than 4096 characters
		; msgbox, 2

		if(BoldTitleWAPI)
		TelegramTitle := "*" . TelegramTitle . "*"
		else, 
		TelegramTitle := TelegramTitle

		Status := SendTelegramPhoto(TelegramBotToken, TelegramBotChatID, ImageAttachmentFilepath, caption := TelegramTitle )       ; you could add more options; compare the Telegram API docs
		Status := SendTelegramMessage(TelegramBotToken, TelegramBotChatID, text := TelegramBody )
	}
	else if(ImageAttachmentFilepath != "" AND StrLen(Message) <= 1020){
		Status := SendTelegramPhoto(TelegramBotToken, TelegramBotChatID, ImageAttachmentFilepath, caption := Message )       ; you could add more options; compare the Telegram API docs

	}
	else, 
	{ ; send just the text
		; Msgbox % "Message: " Message
		Status := SendTelegramMessage(TelegramBotToken, TelegramBotChatID, text := Message )
	}

	if(Status){
		Message = Upload Failed due to API issue. Telegram API Returned Value:`n %Status%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	; SaveOrPostProgress(Message:=Message,PostType:=",ErrorLoggingTextFile,DiscordParler")
	IniWrite, Successful, %StatusFileFilePath%, Status, Telegram
	AddToTotalPostsPostedCount()

	Message = Post Publish Successful
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


	Return
	; -------------------------------/Telegram-------------------------------
