1

Consider this file with arbitrary content:

diamond.txt

When I browse my telegram application select the above file and send it to one of my contacts they get the file with the same name as above (diamond emoji included).

But when I try to send the same file through bot API they get it shown without diamond emoji. They see:

diamond.txt

I have tried to encode the string as 'utf-8', 'ascii' and even url encode but none works.


The code I use:

import json
import requests
import urllib

    
def sendoc(token, chid, pathtodoc, caption:str=None, repkey:dict=None):
    URL   = 'https://api.telegram.org/bot{}/'.format(token)     # Telegram bot API url + TOKEN
    data  = {'chat_id': chid}
    data['caption'] = caption
    if repkey:
        replykey = json.dumps(repkey)
        data['reply_markup'] =replykey
    files = {'document': (pathtodoc, open(pathtodoc, 'rb'))}
    resp = requests.post(URL + 'sendDocument', files = files, data = data)
    resp = resp.json()
    return resp

token = "your bot token"
chid = -1000923413394 # example chat id
pathtodoc = './diamond.txt'
ret = sendoc(token, chid, pathtodoc)
print(ret)

Please help me to send emoji within filename from Bot api.

UPDATE: I find out that there are many other people experiencing similar issue with sending files with non ascii filename through requests module.

See here, here and here.

PouJa
  • 180
  • 4
  • 13
  • 1
    Interesting, seems like every non utf-8 char is removed from the filename. Using postman, with an emoji, escaped or unicode, the are all removed. Documentation doesn't mention anything about accepted filenames. I've asked Telegram Bot Support about this, will post an answer if they reply with something useful! – 0stone0 Jan 18 '23 at 13:02
  • I believe there should be a way. Because when I send the file from telegram mobile app or desktop version the emoji is not removed! My initial thinking is that we need to change some thing in the request header. – PouJa Jan 18 '23 at 14:10
  • @0stone0 Did you hear back from them? Please update me if you know more. – PouJa Feb 12 '23 at 18:51
  • Unfortunately not @PouJa. – 0stone0 Feb 12 '23 at 19:13

0 Answers0