Sending locally hosted photo on telegram bot
Question:
I’m using api.telegram.bot and requests
to send messages and images.
requests.get(url + 'sendMessage', params=dict(chat_id=send_to_user_id,text="Messo"))
This is working fine. My telegram user is able to receive the message “Messo”.
Now, I’m trying to use sendPhoto to send an image that I have hosted on my local drive.
path = "kings/test_screenie1.png"
requests.get(url + 'sendPhoto', params=dict(chat_id=send_to_user_id, photo=open(path,'rb')))
I do not get any exceptions, however, my user is not receiving the image. The output I get in Jupyter notebook is: <Response [414]>
My .ipynb file, where this code is running, is located in: /Users/abc/Desktop/webproject/play0.ipynb
My image file is located in: /Users/abc/Desktop/webproject/kings/test_screenie1.png
I am running this on Mac OS.
Answers:
Please, try this one:
requests.post(url + 'sendPhoto', data={'chat_id': send_to_user_id}, files={'photo': open('/Users/abc/Desktop/webproject/kings/test_screenie1.png', 'rb')})
I have tested locally on my bot, this approach works for me.
Hope, works for you.
Sub telegram_pruebas_photo()
Const URL = "https://api.telegram.org/bot"
Const TOKEN = "5657164377:AAFyybu06zS5_o3ge__gT2XJCh3tqhHIbww"
Const METHOD_NAME = "/sendPhoto?"
Const CHAT_ID = "714106364"
Const FOLDER = "C:UsersPertfectPictures"
Const JPG_FILE = "monkey.png"
Dim data As Object, key
Set data = CreateObject("Scripting.Dictionary")
data.Add "chat_id", CHAT_ID
' generate boundary
Dim BOUNDARY, s As String, n As Integer
For n = 1 To 16: s = s & Chr(65 + Int(Rnd * 25)): Next
BOUNDARY = s & CDbl(Now)
Dim part As String, ado As Object
For Each key In data.keys
part = part & "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""" & key & """" & vbCrLf & vbCrLf
part = part & data(key) & vbCrLf
Next
' filename
part = part & "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""photo""; filename=""" & JPG_FILE & """" & vbCrLf & vbCrLf
' read jpg file as binary
Dim jpg
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1 'binary
ado.Open
ado.LoadFromFile FOLDER & JPG_FILE
ado.Position = 0
jpg = ado.read
ado.Close
' combine part, jpg , end
ado.Open
ado.Position = 0
ado.Type = 1 ' binary
ado.Write ToBytes(part)
ado.Write jpg
ado.Write ToBytes(vbCrLf & "--" & BOUNDARY & "--")
ado.Position = 0
Dim req As Object, reqURL As String
Set req = CreateObject("MSXML2.XMLHTTP")
reqURL = URL & TOKEN & METHOD_NAME
With req
.Open "POST", reqURL, False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & BOUNDARY
.send ado.read
MsgBox .responseText
End With
End Sub
Function ToBytes(str As String) As Variant
Dim ado As Object
Set ado = CreateObject("ADODB.Stream")
ado.Open
ado.Type = 2 ' text
ado.Charset = "_autodetect"
ado.WriteText str
ado.Position = 0
ado.Type = 1
ToBytes = ado.read
ado.Close
End Function
I’m using api.telegram.bot and requests
to send messages and images.
requests.get(url + 'sendMessage', params=dict(chat_id=send_to_user_id,text="Messo"))
This is working fine. My telegram user is able to receive the message “Messo”.
Now, I’m trying to use sendPhoto to send an image that I have hosted on my local drive.
path = "kings/test_screenie1.png"
requests.get(url + 'sendPhoto', params=dict(chat_id=send_to_user_id, photo=open(path,'rb')))
I do not get any exceptions, however, my user is not receiving the image. The output I get in Jupyter notebook is: <Response [414]>
My .ipynb file, where this code is running, is located in: /Users/abc/Desktop/webproject/play0.ipynb
My image file is located in: /Users/abc/Desktop/webproject/kings/test_screenie1.png
I am running this on Mac OS.
Please, try this one:
requests.post(url + 'sendPhoto', data={'chat_id': send_to_user_id}, files={'photo': open('/Users/abc/Desktop/webproject/kings/test_screenie1.png', 'rb')})
I have tested locally on my bot, this approach works for me.
Hope, works for you.
Sub telegram_pruebas_photo()
Const URL = "https://api.telegram.org/bot"
Const TOKEN = "5657164377:AAFyybu06zS5_o3ge__gT2XJCh3tqhHIbww"
Const METHOD_NAME = "/sendPhoto?"
Const CHAT_ID = "714106364"
Const FOLDER = "C:UsersPertfectPictures"
Const JPG_FILE = "monkey.png"
Dim data As Object, key
Set data = CreateObject("Scripting.Dictionary")
data.Add "chat_id", CHAT_ID
' generate boundary
Dim BOUNDARY, s As String, n As Integer
For n = 1 To 16: s = s & Chr(65 + Int(Rnd * 25)): Next
BOUNDARY = s & CDbl(Now)
Dim part As String, ado As Object
For Each key In data.keys
part = part & "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""" & key & """" & vbCrLf & vbCrLf
part = part & data(key) & vbCrLf
Next
' filename
part = part & "--" & BOUNDARY & vbCrLf
part = part & "Content-Disposition: form-data; name=""photo""; filename=""" & JPG_FILE & """" & vbCrLf & vbCrLf
' read jpg file as binary
Dim jpg
Set ado = CreateObject("ADODB.Stream")
ado.Type = 1 'binary
ado.Open
ado.LoadFromFile FOLDER & JPG_FILE
ado.Position = 0
jpg = ado.read
ado.Close
' combine part, jpg , end
ado.Open
ado.Position = 0
ado.Type = 1 ' binary
ado.Write ToBytes(part)
ado.Write jpg
ado.Write ToBytes(vbCrLf & "--" & BOUNDARY & "--")
ado.Position = 0
Dim req As Object, reqURL As String
Set req = CreateObject("MSXML2.XMLHTTP")
reqURL = URL & TOKEN & METHOD_NAME
With req
.Open "POST", reqURL, False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & BOUNDARY
.send ado.read
MsgBox .responseText
End With
End Sub
Function ToBytes(str As String) As Variant
Dim ado As Object
Set ado = CreateObject("ADODB.Stream")
ado.Open
ado.Type = 2 ' text
ado.Charset = "_autodetect"
ado.WriteText str
ado.Position = 0
ado.Type = 1
ToBytes = ado.read
ado.Close
End Function