Jump to content

[SOLVED] _SendDocument missing "parse_mode"


Recommended Posts

Hi guys, i'm using Telegram UDF by @LinkOut from github with latest update, but this UDF has not any parse_mode ability in SendDocument and other send file's functions to make texts bold, italic or underline and i can't send Emojis via these functions too. i've tried to change HTML section of multipart/form-data but i did not get correct results.

For example, i can't get correct results by sending a document with this URL Encoded caption%F0%9F%93%84%20*Test*%20%F0%9F%93%84

I will be happy to help me in this section. Thanks!

Edited by Colduction
Link to comment
Share on other sites

  • Developers

It always helps when you post a replication script as many don't feel the urge to help when they all have to figure it out what you mean and type the code to test.
... but when the option is available in the Telegram bot definition, it should be simple to modify.

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

13 minutes ago, Jos said:

type the code to test

Func _SendDocument($ChatID, $Document, $Caption = '', $ReplyMarkup = Default, $ReplyToMessage = '', $DisableNotification = False)
    Local $Query = $URL & '/sendDocument'
    Local $hOpen = _WinHttpOpen()
    Local $Form = '<form action="' & $Query & '" method="post" enctype="multipart/form-data">' & _
            '<input type="text" name="chat_id"/>' & _
            '<input type="file" name="document"/>' & _
            '<input type="text" name="parse_mode"/>' & _
            '<input type="text" name="caption"/>'
    If $ReplyMarkup <> Default Then $Form &= ' <input type="text" name="reply_markup"/>'
    If $ReplyToMessage <> '' Then $Query &= '<input type="text" name="reply_to_message_id"/>'
    If $DisableNotification Then $Form &= ' <input type="text" name="disable_notification"/>'
    $Form &= '</form>'
    Local $Response = _WinHttpSimpleFormFill($Form, $hOpen, Default, _
            "name:chat_id", $ChatID, _
            "name:document", $Document, _
            "name:parse_mode", "markdown", _
            "name:caption", $Caption, _
            "name:reply_markup", $ReplyMarkup, _
            "name:reply_to_message_id", $ReplyToMessage, _
            "name:disable_notification", $DisableNotification)
    _WinHttpCloseHandle($hOpen)
    Local $json = Json_Decode($Response)
    If Not (Json_IsObject($json)) Then Return SetError($INVALID_JSON_RESPONSE, 0, False) ; JSON Check
    Return __GetFileID($json, 'document')
EndFunc   ;==>_SendDocument

This is what i've tried.

Link to comment
Share on other sites

  • Developers

My guess would be that you need: 

name:parse_mode", "HTML"

.. but if you want me to test it you need to post that requested example script to test with. ;) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

10 minutes ago, Jos said:

My guess would be that you need: 


name:parse_mode", "HTML"

.. but if you want me to test it you need to post that requested example script to test with. ;) 

We are free to select parse_mode style, there is no limit! but one of my problems is that Emojis are not supported in this function, even with URL Encoded emoji!

Link to comment
Share on other sites

  • Developers

Ok....  So?...   Are they at all supported in telegram for that function?

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

9 minutes ago, Jos said:

.I just don't see that test script yet

#include <Telegram.au3>

Global Const $ChatID = '123456789'
Global Const $sToken  = '123456789:AAHezzlggo2NOmS0_o-3oZ4qTtTCs8bLiRg'

_InitBot($sToken)
_SendDocument($ChatID, @DesktopDir & "\1.txt", '%F0%9F%93%84%20*Test*%20%F0%9F%93%84')

 

[EDITED] Telegram UDF.rar

Edited by Colduction
Link to comment
Share on other sites

  • Developers

parse_mode isn't the issue and not required.
It seems that the enctype="multipart/form-data", used for the SendDocument() form is not supporting URLENCODED characters.

Not sure yet how to solve this at the moment.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

I agree it is available in the API, it has to do with the implementation in the udf using a enctype="multipart/form-data

... Which you seem to question?

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

3 hours ago, Jos said:

I agree it is available in the API, it has to do with the implementation in the udf using a enctype="multipart/form-data

... Which you seem to question?

 

I solved one part of my question and now i can send message as markdown parse_mode, but in both before\after editing the source i couldn't send Emojis.

Link to comment
Share on other sites

  • Developers

As stated, it has to do with the way it was implemented in the UDF. Using CURL like this it works fine:

curl -F document=@"1.txt" https://api.telegram.org/bot123456789:AAHezzlggo2NOmS0_o-3oZ4qTtTCs8bLiRg/sendDocument?chat_id=123456789&caption=%F0%9F%93%84%20*Test*%20%F0%9F%93%84

So somebody needs to translate that CURL statement to the proper winhttp syntax.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 minute ago, Jos said:

curl -F document=@"1.txt" https://api.telegram.org/bot123456789:AAHezzlggo2NOmS0_o-3oZ4qTtTCs8bLiRg/sendDocument?chat_id=123456789&caption=%F0%9F%93%84%20*Test*%20%F0%9F%93%84

i've tried this code without passing markdown text as HTML style, but i didn't get good response, it was same.

as you told, i think i should change:

11 minutes ago, Colduction said:

enctype="multipart/form-data

 

Link to comment
Share on other sites

  • Developers
20 minutes ago, Colduction said:

i've tried this code without passing markdown text as HTML style, but i didn't get good response, it was same.

It is working correctly for me as far as I can tell: 

 1031244301_Aantekening2020-07-06192700.jpg.c030ab007a91396c6da5caccba8726ff.jpg

.. or am I missing something?

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jos, I wrote same thing as you:
 

Func _SendDocument($ChatID, $Document, $Caption = '', $ReplyMarkup = Default, $ReplyToMessage = '', $DisableNotification = False)
    Local $Query = $URL & '/sendDocument?parse_mode=markdown&caption=' & $Caption
    Local $hOpen = _WinHttpOpen()
    Local $Form = '<form action="' & $Query & '" method="post" enctype="multipart/form-data">' & _
            '<input type="text" name="chat_id"/>' & _
            '<input type="file" name="document"/>'
    If $ReplyMarkup <> Default Then $Form &= ' <input type="text" name="reply_markup"/>'
    If $ReplyToMessage <> '' Then $Query &= '<input type="text" name="reply_to_message_id"/>'
    If $DisableNotification Then $Form &= ' <input type="text" name="disable_notification"/>'
    $Form &= '</form>'
    Local $Response = _WinHttpSimpleFormFill($Form, $hOpen, Default, _
            "name:chat_id", $ChatID, _
            "name:document", $Document, _
            "name:reply_markup", $ReplyMarkup, _
            "name:reply_to_message_id", $ReplyToMessage, _
            "name:disable_notification", $DisableNotification)
    _WinHttpCloseHandle($hOpen)
    Local $json = Json_Decode($Response)
    If Not (Json_IsObject($json)) Then Return SetError($INVALID_JSON_RESPONSE, 0, False) ; JSON Check
    Return __GetFileID($json, 'document')
EndFunc   ;==>_SendDocument

But as you told, i should think about enctype="multipart/form-data"

Link to comment
Share on other sites

@Jos, I've fixed both Emojis and Markdown style by adding _URLDecode() function, but now i have new problem, i can't send %0D%0A to make new line, WinHTTP replace %20 instead of %0D%0A
 

Func _SendDocument($ChatID, $Document, $Caption = '', $ReplyMarkup = Default, $ReplyToMessage = '', $DisableNotification = False)
    Local $Query = $URL & '/sendDocument?chat_id=' & $ChatID & '&parse_mode=markdown&caption=' & _URLDecode($Caption)
    Local $hOpen = _WinHttpOpen()
    Local $Form = '<form action="' & $Query & '" method="post" enctype="multipart/form-data">' & '<input type="file" name="document"/>'
    If $ReplyMarkup <> Default Then $Form &= ' <input type="text" name="reply_markup"/>'
    If $ReplyToMessage <> '' Then $Query &= '<input type="text" name="reply_to_message_id"/>'
    If $DisableNotification Then $Form &= ' <input type="text" name="disable_notification"/>'
    $Form &= '</form>'
    Local $Response = _WinHttpSimpleFormFill($Form, $hOpen, Default, _
            "name:document", $Document, _
            "name:reply_markup", $ReplyMarkup, _
            "name:reply_to_message_id", $ReplyToMessage, _
            "name:disable_notification", $DisableNotification)
    _WinHttpCloseHandle($hOpen)
    Local $json = Json_Decode($Response)
    If Not (Json_IsObject($json)) Then Return SetError($INVALID_JSON_RESPONSE, 0, False) ; JSON Check
    Return __GetFileID($json, 'document')
EndFunc   ;==>_SendDocument

Func _URLDecode($sData)
    Local $aData = StringSplit(StringReplace($sData, "%20", " ", 0, 1), "%")
    $sData = ""
    For $i = 2 To $aData[0]
        $aData[1] &= Chr(Dec(StringLeft($aData[$i], 2))) & StringTrimLeft($aData[$i], 2)
    Next
    Return BinaryToString(StringToBinary($aData[1], 1), 4)
EndFunc   ;==>_URLDecode

 

Edited by Colduction
Link to comment
Share on other sites

  • Developers

You can make it all work fine without the _UrlDecode when you apply the following changes:

 Telegram.au3:

Func _SendDocument($ChatID, $Document, $Caption = '', $ReplyMarkup = Default, $ReplyToMessage = '', $DisableNotification = False)
    Local $Query = $URL & '/sendDocument?chat_id=' & $ChatID & '&parse_mode=markdown&caption=' & $Caption
    If $ReplyMarkup <> Default Then $Query &= "&reply_markup=" & $ReplyMarkup
    If $ReplyToMessage <> '' Then $Query &= "&reply_to_message_id=" & $ReplyToMessage
    If $DisableNotification Then $Query &= "&disable_notification=" & $DisableNotification
    Local $hOpen = _WinHttpOpen()
    Local $Form = '<form action="' & $Query & '" method="post" enctype="multipart/form-data"><input type="file" name="document"/>'
    $Form &= '</form>'
    Local $Response = _WinHttpSimpleFormFill($Form, $hOpen, Default, _
            "name:document", $Document)
    _WinHttpCloseHandle($hOpen)
    Local $json = Json_Decode($Response)
    If Not (Json_IsObject($json)) Then Return SetError($INVALID_JSON_RESPONSE, 0, False) ; JSON Check
    Return __GetFileID($json, 'document')
EndFunc   ;==>_SendDocument

winhttp:au3:

Func __WinHttpNormalizeActionURL($sActionPage, ByRef $sAction, ByRef $iScheme, ByRef $sNewURL, ByRef $sEnctype, ByRef $sMethod, $sURL = "")
;~  Local $aCrackURL = _WinHttpCrackUrl($sAction)
    Local $aCrackURL = _WinHttpCrackUrl($sAction, $ICU_DECODE)

This issue is that the latter UDF call to _WinHttpCrackUrl($sAction) will perform a urlencode by default which is changed by adding  the second parameter to make it do the reverse.  Don't make this change a permanent one in the standard winhttp.au3, but for me now all works fine... both the emoji's and %0a for a newline.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Colduction changed the title to [SOLVED] _SendDocument missing "parse_mode"

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...