我正在尝试使用用户发送到我的机器人的图像来创建贴纸集。我有以下代码:
MY_USER_ID = "MyFakeUserId"
MY_TOKEN = "MyFakeToken"
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
updater = Updater(token=MY_TOKEN, use_context=True)
updater.start_polling()
dispatcher = updater.dispatcher
def create_sticker_set(update, context):
update_dict = update.to_dict()
sticker_file_id = update_dict["message"]["photo"][-1]["file_id"]
print("sticker_file_id=", sticker_file_id)
# sticker_file = context.bot.upload_sticker_file(MY_USER_ID, sticker_file_id)
context.bot.create_new_sticker_set(MY_USER_ID, "lala_by_my_fake_bot", "lala", sticker_file_id, "???")
msghandler = MessageHandler(Filters.photo, create_sticker_set)
dispatcher.add_handler(msghandler)
我发送聊天图片大小为512px x 512px的png图片。而且此代码不起作用。
我在日志中收到以下消息:
sticker_file_id= AgACAgQAAx0CUsT2sgADyF5SvWsoQ9ZTjw436M0nntrlkUtkAALjsTEb9W2ZUg-6JVL7B5D8wjq2GwAEAQADAgADeAADyYoDAAEYBA
2020-02-23 18:59:07,376 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/telegram/ext/dispatcher.py", line 390, in process_update
handler.handle_update(update, self, check, context)
File "/usr/local/lib/python3.7/site-packages/telegram/ext/handler.py", line 117, in handle_update
return self.callback(update, context)
File "testing.py", line 58, in callback
context.bot.create_new_sticker_set(MY_USER_ID, "facebooklala_by_ibodi_bot", "FacebookLala", sticker_file_id, "???")
File "</usr/local/lib/python3.7/site-packages/decorator.py:decorator-gen-58>", line 2, in create_new_sticker_set
File "/usr/local/lib/python3.7/site-packages/telegram/bot.py", line 67, in decorator
result = func(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/telegram/bot.py", line 3298, in create_new_sticker_set
result = self._request.post(url, data, timeout=timeout)
File "/usr/local/lib/python3.7/site-packages/telegram/utils/request.py", line 334, in post
**urlopen_kwargs)
File "/usr/local/lib/python3.7/site-packages/telegram/utils/request.py", line 245, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Type of file mismatch
除了下载图片然后重新上传之外,我看不到其他方法。像这样:
def create_sticker_set(update, context):
update_dict = update.to_dict()
sticker_file_id = update_dict["message"]["photo"][-1]["file_id"]
print("sticker_file_id=", sticker_file_id)
# sticker_file = context.bot.upload_sticker_file(MY_USER_ID, sticker_file_id)
file = context.bot.get_file(update_dict["message"]["photo"][-1]["file_id"])
filename = file.download()
context.bot.create_new_sticker_set(MY_USER_ID, "lala_by_ibodi_bot", "lala", open(filename, "rb"), "???")
答案 0 :(得分:0)
我找到了答案。将png文件上传为照片而不是文件,会将其转换为jpg。 sticker_file_id
不是context.bot.create_new_sticker_set
的正确参数,因为它是jpg文件的file_id
,应该是png文件的file_id
。
此代码应在我们将图片上传为文件而不是图片的情况下起作用。
MY_USER_ID = "MyFakeUserId"
MY_TOKEN = "MyFakeToken"
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
updater = Updater(token=MY_TOKEN, use_context=True)
updater.start_polling()
dispatcher = updater.dispatcher
def create_sticker_set(update, context):
update_dict = update.to_dict()
sticker_file_id = update_dict["message"]["document"]["file_id"]
print("sticker_file_id=", sticker_file_id)
# sticker_file = context.bot.upload_sticker_file(MY_USER_ID, sticker_file_id)
context.bot.create_new_sticker_set(MY_USER_ID, "lala_by_my_fake_bot", "lala", sticker_file_id, "???")
msghandler = MessageHandler(Filters.document, create_sticker_set)
dispatcher.add_handler(msghandler)