To send an MP4 video file from an FTP server to a Telegram channel using Python, you will need a library requests
to interact with the Telegram API and a library ftplib
to work with FTP. Below is an example script that does this.
Example script
import os
import requests
from ftplib import FTP
# Настройки FTP
ftp_host = 'ftp.example.com'
ftp_user = 'your_username'
ftp_pass = 'your_password'
video_file_name = 'video.mp4'
# Настройки Telegram
bot_token = 'ваш_токен_бота'
chat_id = '@ваш_канал' # или ID канала
# Подключение к FTP и загрузка видеофайла
def download_video_from_ftp():
ftp = FTP(ftp_host)
ftp.login(ftp_user, ftp_pass)
with open(video_file_name, 'wb') as local_file:
ftp.retrbinary(f'RETR {video_file_name}', local_file.write)
ftp.quit()
# Отправка видео в Telegram
def send_video_to_telegram():
with open(video_file_name, 'rb') as video_file:
files = {'document': video_file}
response = requests.post(
f'https://api.telegram.org/bot{bot_token}/sendDocument',
data={'chat_id': chat_id},
files=files
)
return response.json()
# Основной процесс
if __name__ == '__main__':
download_video_from_ftp()
response = send_video_to_telegram()
print(response)
Code Explanation
- FTP Settings : Enter your FTP server address, username and password for access.
- Telegram settings : Specify your bot token and the channel ID where the video will be sent.
- Function
download_video_from_ftp
: Connects to an FTP server and downloads the specified video file to your local machine. - Function
send_video_to_telegram
: Opens the downloaded video file and sends it to the specified Telegram channel via API. - Main process : Downloads video from FTP and sends it to Telegram.
Dependencies
Make sure you have the required libraries installed:
pip install requests
This script allows you to automate the process of sending videos from FTP to a Telegram channel. Don’t forget to replace the parameters with your own.
Citations:
[1] https://qna.habr.com/q/948043
[2] https://stackoverflow.com/questions/69515206/how-can-i-send-a-video-to-telegram-channel-with-python
[3] https://qna.habr.com/q/962739
[4] https://ramziv.com/article/19
[5] https://www.youtube.com/watch?v=d0doAjCtxHM
[6] https://ru.stackoverflow.com/questions/931492/%D0%9E%D1%82%D0%BF%D1%80%D0%B0%D0%B2%D0%BA%D0%B0-%D1%81%D0%BE%D0%BE%D0%B1%D1%89%D0%B5%D0%BD%D0%B8%D1%8F-%D0%B2-%D0%BA%D0%B0%D0%BD%D0%B0%D0%BB-telegram-%D1%81%D1%80%D0%B5%D0%B4%D1%81%D1%82%D0%B2%D0%B0%D0%BC%D0%B8-python
[7] https://www.youtube.com/watch?v=-73hRicngD8
[8] https://sendsay.ru/api/api.html