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

roy*_*roy 5 python telegram python-telegram-bot telegram-bot

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

Sim*_* C. 4

您的问题是send_photo(chat_id, pic)函数的第二个参数应该是 afile id而不是您正在执行的文件的名称(参见doc)。

您可以简单地更改您的代码:

 bot.send_photo(chat_id, pic)
Run Code Online (Sandbox Code Playgroud)

这样:

bot.send_photo(chat_id, open(pic,'rb'))
Run Code Online (Sandbox Code Playgroud)