how to send a local file in bale bot to a client?
Question:
I’ve tried some code but I was not successful to send a file message with my bot. After upload my file I’m not able to get file_id and access_hash and other from the server to send it.
It’s my code:
@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)
It doesn’t work! 🙁
any help?
Answers:
You should use success callback to handle what you need after uploading the file.Result and user_data are parameters you can get file_id and access_hash from.
here is a complete example:
@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))
I think this code can help you
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()
in python-bale-bot, you can follows these steps:
- First, send the file to the bot in Bale messenger in order to find its
file_id
.
@bot.listen(bale.EventType.MESSAGE)
async def on_callback(message: bale.Message):
if message.document:
print(message.document.file_id)
- Now that we have the
file_id
, we can send the file to them, as if the bot is forwarding a document message.
@bot.listen(bale.EventType.CALLBACK)
async def on_callback(callback: bale.CallbackQuery):
await callback.message.chat.send_document(document = "Your file id", ...)
In this method, there is no need to open the file in your python script. Your file can be recognized using its ID.
I’ve tried some code but I was not successful to send a file message with my bot. After upload my file I’m not able to get file_id and access_hash and other from the server to send it.
It’s my code:
@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)
It doesn’t work! 🙁
any help?
You should use success callback to handle what you need after uploading the file.Result and user_data are parameters you can get file_id and access_hash from.
here is a complete example:
@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))
I think this code can help you
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()
in python-bale-bot, you can follows these steps:
- First, send the file to the bot in Bale messenger in order to find its
file_id
.
@bot.listen(bale.EventType.MESSAGE)
async def on_callback(message: bale.Message):
if message.document:
print(message.document.file_id)
- Now that we have the
file_id
, we can send the file to them, as if the bot is forwarding a document message.
@bot.listen(bale.EventType.CALLBACK)
async def on_callback(callback: bale.CallbackQuery):
await callback.message.chat.send_document(document = "Your file id", ...)
In this method, there is no need to open the file in your python script. Your file can be recognized using its ID.