【问题标题】:how to send a local file in bale bot to a client?如何将bale bot中的本地文件发送给客户端?
【发布时间】:2018-11-07 05:44:20
【问题描述】:

我尝试了一些代码,但未能成功使用我的机器人发送文件消息。上传我的文件后,我无法从服务器获取 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)

这行不通! :( 有什么帮助吗?

【问题讨论】:

    标签: python bots bale-messenger


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      上传文件后,您应该使用成功回调来处理您需要的内容。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))
      

      【讨论】:

      • 您似乎在代码中发送了照片消息,但问题询问有关上传文件的问题
      • 感谢您的回答 Ehsan,您的回答也很有用
      猜你喜欢
      • 2019-05-08
      • 1970-01-01
      • 2018-11-27
      • 2018-10-27
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多