gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:04 25 4
gpt4 key购买 nike

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

关于python - 如何通过 python-telegram-bot 发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com