gpt4 book ai didi

python - 如何将 bale bot 中的本地文件发送给客户端?

转载 作者:行者123 更新时间:2023-11-28 20:14:19 28 4
gpt4 key购买 nike

我已经尝试了一些代码,但我没有成功地用我的机器人发送文件消息。上传我的文件后,我无法从服务器获取 file_id 和 access_hash 等来发送它。这是我的代码:

@dispatcher.message_handler(PhotoFilter())
def ask_photo(bot, update):
user_peer = update.get_effective_user()
bot.upload_file(file="../files/upload_file_test.jpeg",
file_type="file",
success_callback=file_upload_success,failure_callback=failure)


def file_upload_success(response):
photo_message = PhotoMessage(file_id=response.file_id,
access_hash=response.access_hash, name="photo", file_size="100",
mime_type="image/jpeg", thumb=response.thumb, width=80, height=80,
caption_text="caption")
bot.send_message(photo_message, user_peer)

没用! :( 有帮助吗?

最佳答案

我认为这段代码可以帮到你

import asyncio
from balebot.filters import
from balebot.models.messages import
from balebot.updater import Updater

updater = Updater(token="PUT YOUR TOKEN HERE",loop=asyncio.get_event_loop())
bot = updater.bot
dispatcher = updater.dispatcher

def success(response, user_data):
print("success : ", response)
print(user_data)

def failure(response, user_data):
print("failure : ", response)
print(user_data)

@dispatcher.message_handler(DefaultFilter())
def send_txt_file(bot, update):
user_peer = update.get_effective_user()

def file_upload_success(result, user_data):
print("upload was successful : ", result)
print(user_data)

file_id = str(user_data.get("file_id", None))
access_hash = str(user_data.get("user_id", None))
file_message = DocumentMessage(file_id=file_id, access_hash=access_hash, name="Bale", file_size='PUT YOUR FILE SIZE HERE',
mime_type="PUT YOUR FILE MIME TYPE HERE", caption_text=TextMessage(text="HELLO"),
file_storage_version=1)
bot.send_message(file_message, user_peer, success_callback=success, failure_callback=failure)

bot.upload_file(file="PUT YOUR FILE LOCATION ADDRESS HERE", file_type="file", success_callback=file_upload_success,
failure_callback=failure)

updater.run()

关于python - 如何将 bale bot 中的本地文件发送给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569618/

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