Skip to main content
 首页 » 编程设计

python之如何通过 python-telegram-bot 发送照片

2024年12月31日7三少

我希望代码在用户点击/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()) 

请您参考如下方法:

你的问题是 send_photo(chat_id, pic) 函数的第二个参数应该是一个 file id 而不是 name你正在做的文件(参见 doc )。

你可以简单地改变你的代码:

 bot.send_photo(chat_id, pic) 

通过这个:

bot.send_photo(chat_id, open(pic,'rb'))