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

时间:2018-05-28 15:34:00

标签: python bots bale-messenger

我尝试了一些代码但是我没有成功用我的机器人发送文件消息。上传我的文件后,我无法从服务器获取file_id和access_hash以及其他信息。 这是我的代码:

{{1}}

它不起作用! :(  任何帮助?

2 个答案:

答案 0 :(得分:4)

您应该使用成功回调来处理上传文件后所需的内容.Result和user_data是您可以从中获取file_id和access_hash的参数。 这是一个完整的例子:

@dispatcher.message_handler(PhotoFilter())
def ask_photo(bot, update):
    user_peer = update.get_effective_user()

    def file_upload_success(result, user_data):
        """Its the link of upload photo but u cant see anything with it because you need to take a token from server.
            actually this link is just for uploading a file not download. If you want to download this file you should
            use get_file_download_url() and take a token from server.
        """
        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))
        print("fileIDD: ", file_id, "access", access_hash)
        v_message = PhotoMessage(file_id=file_id, access_hash=access_hash, name="Bale", file_size='11337',
                                 mime_type="image/jpeg", caption_text=TextMessage(text="Bale"),
                                 file_storage_version=1, thumb=None)

        bot.send_message(v_message, user_peer, success_callback=success, failure_callback=failure)

    bot.upload_file(file="../files/upload_file_test.jpeg", file_type="file", success_callback=file_upload_success,
                    failure_callback=failure)
    message = TextMessage("Thanks \nplease send a Hello voice message.")
    user_peer = update.get_effective_user()
    bot.send_message(message, user_peer, success_callback=success, failure_callback=failure)
    dispatcher.register_conversation_next_step_handler(update, MessageHandler(VoiceFilter(), finish_conversion))

答案 1 :(得分:4)

我认为此代码可以帮助您

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()