gpt4 book ai didi

python - 如何使用python在 Telegram 上发送照片

转载 作者:行者123 更新时间:2023-12-05 02:07:17 25 4
gpt4 key购买 nike

当我发出命令/screen 时,我正在尝试将我计算机上的照片发送到我的 Telegram Bot 聊天。但是,我似乎没有让它发挥作用,我错过了什么?

这是我正在使用的文件,抱歉,它有点长,但我想展示所有内容,因为我可能在某处遗漏了一些东西。从 ###### HERE I WANNA TRY TO SEND THE PHOTO ###### 行看,这样您就可以看到我在哪里定义了 send_photo 部分。

import os
import bot
import schedule
import random
from time import sleep
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')

###### HERE I WANNA TRY TO SEND THE PHOTO ######
###### HERE I WANNA TRY TO SEND THE PHOTO ######
def screen(update, context):
"""Taking a screenshot when command /screen is issued."""
update.message.reply_text('Taking screenshot ...')
os.system("adb exec-out screencap -p > /Users/????/Desktop/AndroBot/screen.png")
update.message.reply_text('Screenshot taken!')
#HEREEEEE
update.message.send_photo(?????)

def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater("TOKEN", use_context=True)

# Get the dispatcher to register handlers
dp = updater.dispatcher

# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("screen", screen))

# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))

# log all errors
dp.add_error_handler(error)

# Start the Bot
updater.start_polling()

# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()

if __name__ == '__main__':
main()

我希望有人能帮助我...

最佳答案

来自 documentation :

send_photo(chat_id, photo, caption=None, disable_notification=False, reply_to_message_id=None, reply_markup=None, timeout=20, parse_mode=None, **kwargs)

所以你的代码应该是这样的:

filename = "/Users/????/Desktop/AndroBot/screen.png"
os.system("adb exec-out screencap -p > "+filename)
update.message.bot.send_photo(update.message.chat.id,open(filename,'rb'))

关于python - 如何使用python在 Telegram 上发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61923014/

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