如何通过python电报机器人发送照片
我希望代码发送一张照片,一旦用户点击/start命令,它不工作,任何人都可以帮助我。 多谢各位如何通过python电报机器人发送照片,python,telegram,telegram-bot,python-telegram-bot,Python,Telegram,Telegram Bot,Python Telegram Bot,我希望代码发送一张照片,一旦用户点击/start命令,它不工作,任何人都可以帮助我。 多谢各位 import emoji, telegram from telegram.ext import Updater from telegram.ext import CommandHandler, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup token = "----------
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\u photo(chat\u id,pic)
函数的第二个参数应该是文件id
,而不是您正在执行的文件名(参见)
您只需更改代码即可:
bot.send_photo(chat_id, pic)
据此:
bot.send_photo(chat_id, open(pic,'rb'))
bot.send\u photo
方法在电报服务器上接受文件对象或文件ID作为photo参数,因此您应该传递类似于open(pic,“rb”)
的内容,而不是简单的pic
第一次发送照片后,您可以从回复中获取该照片的电报文件id。
传递电报的文件id而不是文件对象更快,因为您只需将电报指向它已经拥有的文件,而不需要反复读取和发送同一文件
您可以查找文档此库已经有完整的文档我最近发布了一个关于阅读的问题的错误答案(发送):