我想要的代码发送一张照片,一旦用户点击/start命令,它不工作,任何人可以帮助我。谢谢
import emoji, telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
token = "-----------"
bot = telegram.Bot(token=token)
pic = "./photo.png"
try:
chat_id = bot.get_updates()[-1].message.chat_id
except IndexError:
chat_id = 0
#general functions
def start(bot, update):
bot.send_photo(chat_id, pic)
update.message.reply_text(text="helo",
reply_markup=menu_keyboard())发布于 2018-11-29 18:26:37
您的问题是,send_photo(chat_id, pic)函数的第二个参数应该是一个file id,而不是您正在做的文件的名称(参见doc)。
您可以简单地更改您的代码:
bot.send_photo(chat_id, pic)通过以下方式:
bot.send_photo(chat_id, open(pic,'rb'))发布于 2019-11-04 17:23:59
bot.send_photo方法接受文件对象或电报服务器上文件的ID作为照片参数,因此您应该传递类似open(pic, "rb")的内容,而不是简单的pic。
一旦照片第一次被发送,你就可以从响应中获得它的电报文件id。传递Telegram的文件id而不是file对象会更快,因为您只需将Telegram指向它已有的文件,而不是一遍又一遍地读取和发送同一文件。
您可以查找文档here
https://stackoverflow.com/questions/51222907
复制相似问题