无法使用 Telegram api 和 python 获取图像信息
Can't get image information with Telegram api and python
我制作了一个机器人来接收图像到 python
并与它们一起工作,一直到 heroku
。
我做了一个简单的页面来查看收到的信息是这样的:
@app.route('/apunteswaw',methods=['POST'])
def apunteswaw():
data = json.loads(flask.request.data)
...
@app.route('/recive',methods=['GET'])
def recive():
return 'Mensaje: {}'.format(data)
页面显示最后发送的消息,但是当我发送图片时,它会跳过它并显示最后一条短信,如下所示:
{'update_id': 54485500, 'message': {'message_id': 12, 'from': {'id': 231951870, 'is_bot': False, 'first_name': '***', 'last_name': '***', 'language_code': 'en-US'}, 'chat': {'id': 231951870, 'first_name': '***', 'last_name': '***', 'type': 'private'}, 'date': 1525151196, 'text': 'asdasd'}}
如何通过 bot
接收照片数据以下载从我的 telegram
应用程序发送的照片?
深入挖掘后,我得出结论,Telegram API 只是不将带有媒体的消息发送到 WebHook(也许这只是我的情况,但我只是不知道)。
要获取最后一条消息的图片,您必须禁用 webhook,并使用 getUpdates
方法。
updates = requests.get('http://api.telegram.org/bot{}/getUpdates'.format(token))
if updates.status_code ==200:
data = json.loads(updates.text)
last = data['result'][-1]
ide = ''
if 'photo' in last['message']:
foto = True
ide = last['message']['photo'][2]['file_id']
else:
foto = False
print('no photo')
return foto, ide
从那里,响应包括照片 ID 以使用方法 getFile
:
检索它
r = requests.get('http://api.telegram.org/bot{}/getFile?file_id={}'.format(token,ide),)
if r.status_code == 200:
path = json.loads(r.text)
return path['result']['file_path']
终于有了可以下载图片的路径:
'http://api.telegram.org/file/bot<token>/<path>
获取音频、视频等其他媒体文件同上,只是在getUpdates
的响应字典的另一个路径中找到了id
我制作了一个机器人来接收图像到 python
并与它们一起工作,一直到 heroku
。
我做了一个简单的页面来查看收到的信息是这样的:
@app.route('/apunteswaw',methods=['POST'])
def apunteswaw():
data = json.loads(flask.request.data)
...
@app.route('/recive',methods=['GET'])
def recive():
return 'Mensaje: {}'.format(data)
页面显示最后发送的消息,但是当我发送图片时,它会跳过它并显示最后一条短信,如下所示:
{'update_id': 54485500, 'message': {'message_id': 12, 'from': {'id': 231951870, 'is_bot': False, 'first_name': '***', 'last_name': '***', 'language_code': 'en-US'}, 'chat': {'id': 231951870, 'first_name': '***', 'last_name': '***', 'type': 'private'}, 'date': 1525151196, 'text': 'asdasd'}}
如何通过 bot
接收照片数据以下载从我的 telegram
应用程序发送的照片?
深入挖掘后,我得出结论,Telegram API 只是不将带有媒体的消息发送到 WebHook(也许这只是我的情况,但我只是不知道)。
要获取最后一条消息的图片,您必须禁用 webhook,并使用 getUpdates
方法。
updates = requests.get('http://api.telegram.org/bot{}/getUpdates'.format(token))
if updates.status_code ==200:
data = json.loads(updates.text)
last = data['result'][-1]
ide = ''
if 'photo' in last['message']:
foto = True
ide = last['message']['photo'][2]['file_id']
else:
foto = False
print('no photo')
return foto, ide
从那里,响应包括照片 ID 以使用方法 getFile
:
r = requests.get('http://api.telegram.org/bot{}/getFile?file_id={}'.format(token,ide),)
if r.status_code == 200:
path = json.loads(r.text)
return path['result']['file_path']
终于有了可以下载图片的路径:
'http://api.telegram.org/file/bot<token>/<path>
获取音频、视频等其他媒体文件同上,只是在getUpdates