API Telegram
Moderator: Rathinagiri
API Telegram
Hi,
I'm testing to make Telegram API calls for bot (based on our friend Riztan's source code)
I'm having trouble with the sendPhoto function since as I understand it requires converting the png image file to base64, so I'm trying something like this:
local cImgLocalPath := "c:\bot_javier\image.png"
cImgFileContent := FileStr( cImgLocalPath )
cBase64ContentFile := hb_Base64Encode( cImgFileContent, Len( cImgFileContent ) )
oBot:sendPhoto( chat_id, "caption", cBase64ContentFile, "" )
but I get this error response: do we have some function to convert the image file to base64?, I'm doing something wrong with?.
I appreciate any help.
Regards,
Javier
I'm testing to make Telegram API calls for bot (based on our friend Riztan's source code)
I'm having trouble with the sendPhoto function since as I understand it requires converting the png image file to base64, so I'm trying something like this:
local cImgLocalPath := "c:\bot_javier\image.png"
cImgFileContent := FileStr( cImgLocalPath )
cBase64ContentFile := hb_Base64Encode( cImgFileContent, Len( cImgFileContent ) )
oBot:sendPhoto( chat_id, "caption", cBase64ContentFile, "" )
but I get this error response: do we have some function to convert the image file to base64?, I'm doing something wrong with?.
I appreciate any help.
Regards,
Javier
- AUGE_OHR
- Posts: 2117
- Joined: Sun Aug 25, 2019 3:12 pm
- DBs Used: DBF, PostgreSQL, MySQL, SQLite
- Location: Hamburg, Germany
Re: API Telegram
hi,
this is in Demo CODE
i don´t "see" any "encode"
this is in Demo CODE
Code: Select all
procedure bot_xBase( oFrom, oUpdate, oMsg, oBot )
local cPathImg := "http://www.harbour-project.com.br/art/harbour-logo.jpg"
local cText
if oUpdate:isDef("message")
tracelog "This is a message"
elseif oUpdate:isDef("callback_query")
tracelog "This is a callback query"
endif
tracelog "chat id: "+oMsg:chat:id
tracelog "from user (id): "+oFrom:id
cText := "<b>The Harbour Project</b>"+hb_eol()
cText += "<i>Clipper compatible OpenSource Compiler</i>"+hb_eol()
cText += "Harbour Reference Guide: https://harbour.github.io/doc/harbour.html"
oBot:sendPhoto( oMsg:chat:id, cText, cPathImg )
returnhave fun
Jimmy
Jimmy
-
brunellopulix
- Posts: 80
- Joined: Sat Apr 24, 2010 10:17 am
Re: API Telegram
Hi
local cImgLocalPath := "c:\bot_javier\image.png"
cImgFileContent := hb_MemoRead( cImgLocalPath )
cBase64ContentFile := hb_Base64Encode( cImgFileContent, 57 )
oBot:sendPhoto( chat_id, "caption", cBase64ContentFile, "" )
Brunello Pulix
local cImgLocalPath := "c:\bot_javier\image.png"
cImgFileContent := hb_MemoRead( cImgLocalPath )
cBase64ContentFile := hb_Base64Encode( cImgFileContent, 57 )
oBot:sendPhoto( chat_id, "caption", cBase64ContentFile, "" )
Brunello Pulix
Re: API Telegram
I'm sorry Brunello, but with this code I keep getting the same error response.
I appreciate your help.
Regards,
Javier
I appreciate your help.
Regards,
Javier
- serge_girard
- Posts: 3420
- Joined: Sun Nov 25, 2012 2:44 pm
- DBs Used: 1 MySQL - MariaDB
2 DBF - Location: Belgium
- Contact:
Re: API Telegram
Javier,
Did you check exact error? Wrong remote file identifier specified: Wrong character in the string.
Maybe see: https://github.com/telegraf/telegraf/issues/886
Did you check exact error? Wrong remote file identifier specified: Wrong character in the string.
Maybe see: https://github.com/telegraf/telegraf/issues/886
There's nothing you can do that can't be done...
Re: API Telegram
Hi Jimmy,AUGE_OHR wrote: ↑Sat Oct 01, 2022 8:19 am hi,
this is in Demo CODEi don´t "see" any "encode"Code: Select all
procedure bot_xBase( oFrom, oUpdate, oMsg, oBot ) local cPathImg := "http://www.harbour-project.com.br/art/harbour-logo.jpg" local cText if oUpdate:isDef("message") tracelog "This is a message" elseif oUpdate:isDef("callback_query") tracelog "This is a callback query" endif tracelog "chat id: "+oMsg:chat:id tracelog "from user (id): "+oFrom:id cText := "<b>The Harbour Project</b>"+hb_eol() cText += "<i>Clipper compatible OpenSource Compiler</i>"+hb_eol() cText += "Harbour Reference Guide: https://harbour.github.io/doc/harbour.html" oBot:sendPhoto( oMsg:chat:id, cText, cPathImg ) return
The demo works by sending an image from a link on the web, but that example doesn't work if you need to send an image from your local PC.
I have seen a bit of your post:
https://hmgforum.com/viewtopic.php?f=5& ... ink#p68501
It seems to me that it is related to what I am looking for, because according to the infinity of examples that I have seen with both curl and http, they require to convert an image or a document to its content in byteArray, in fact I have examples working with VBA Excel, but for me they are complicated to "translate" to Harbour.
So, can you share how you solved the binary issue with your Microsoft Ink project, maybe I can try something similar and see if it works.
I appreciate any help.
Regards,
Javier
Re: API Telegram
When browsing the sources of the hbtelegram class https://github.com/riztan/hbtelegram/bl ... _class.prg, it seems that we do not encode the image file bytes, but only provide the full path to the file that we upload to the server.
Okey. I've looked at this class in more detail, and it doesn't allow file uploads using multipart/form-data required for file upload from computer.
https://core.telegram.org/bots/api#sendphoto
You have to build the request yourself, it should look something like this:
I haven't tested so it's just a tip, I'm not using this class and I'm not going to be yet.
PS.
This class seems underdeveloped. See for example the :sendVoiceFile method. It uses the external curl command instead of the native libcurl commands.
Code: Select all
METHOD sendPhoto (uChatId, cCaption, cImagePath, oMsg, hOptionals)Okey. I've looked at this class in more detail, and it doesn't allow file uploads using multipart/form-data required for file upload from computer.
https://core.telegram.org/bots/api#sendphoto
You have to build the request yourself, it should look something like this:
Code: Select all
cChat_id := oMsg:chat:id /* Chat ID */
cCaption := "Any caption"
cPhotoFile := "c:\bot_javier\image.png"
boundary:='------' + hb_StrToHex ( hb_TtoC( hb_DateTime(), 'YYYYMMDDhhmmssfff' ) )
cFormBody:='--' + boundary + CRLF
cFormBody+='Content-Disposition: form-data; name="chat_id"' + CRLF + CRLF
cFormBody+= cChat_id + CRLF
cFormBody:='--' + boundary + CRLF
cFormBody+='Content-Disposition: form-data; name="caption"' + CRLF + CRLF
cFormBody+= cCaption + CRLF
cFormBody+='--' + boundary + CRLF
cFormBody+='Content-Disposition: form-data; name="photo"; filename="' + hb_FNameNameExt ( cPhotoFile ) + '"' + CRLF
SWITCH Upper(hb_FNameExt ( cPhotoFile ))
CASE "JPG"
cContentType:='image/jpeg'
EXIT
CASE "JPEG"
cContentType:='image/jpeg'
EXIT
CASE "BMP"
cContentType:='image/bmp'
EXIT
CASE "PNG"
cContentType:='image/png'
EXIT
OTHERWISE
cContentType:='image/jpeg'
END SWITCH
cFormBody += 'Content-Type: ' + cContentType + CRLF + CRLF
cFormBody += FILESTR( cPhotoFile ) + CRLF
cFormBody+='--' + boundary + '--' + CRLF
aHeaders := { "Content-Length: " + Str(Len(cFormBody)) , ;
"Content-Type: " + "multipart/form-data; boundary=" + boundary }
curl_global_init()
pCurl := curl_easy_init()
/* Specify the Header data */
curl_easy_setopt( pCurl, HB_CURLOPT_HTTPHEADER, aHeaders)
curl_easy_setopt( pCurl, HB_CURLOPT_URL, oBot:cUrl + "sendPhoto" )
curl_easy_setopt( pCurl, HB_CURLOPT_FOLLOWLOCATION, .T. )
curl_easy_setopt( pCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
curl_easy_setopt( pCurl, HB_CURLOPT_DOWNLOAD )
curl_easy_setopt( pCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( pCurl, HB_CURLOPT_POST, .T. )
curl_easy_setopt( pCurl, HB_CURLOPT_POSTFIELDSIZE, LEN( cFormBody ) )
curl_easy_setopt( pCurl, HB_CURLOPT_POSTFIELDS, cFormBody )
nResp := curl_easy_perform( pCurl ) /* Do everything */
/* Look at execute method class TLGRMBOT todo get response or catch errors */
PS.
This class seems underdeveloped. See for example the :sendVoiceFile method. It uses the external curl command instead of the native libcurl commands.
Re: API Telegram
There are no words to show gratitude, thank you very much... it worked perfect Edward!
I really appreciate your time and help.
Regards,
Javier
I really appreciate your time and help.
Regards,
Javier