This is a Python-based bot for Telegram that allows you to automatically send messages and images to a specified chat or channel on a scheduled basis. The bot can send posts either monthly on a specific day and time or periodically at a specified interval.
Scheduled Posting: Send messages and images to a Telegram chat or channel at a specific time or interval.
Uses async/await for all I/O operations
Proper use of the context manager for a bot
In periodic mode: precise intervals
In monthly mode: check every minute
Separate Telegram API error handling
Asynchronous logging with writing to a file
Text message from a .txt file.
Image from a .png file.
Monthly posting on a specific day and time.
Periodic posting at a specified interval (e.g., every 1 hour).
Logging: All actions are logged in a file for easy debugging and tracking.
Before running the bot, ensure you have the following:
Python 3.7 or higher.
Create a Telegram bot using BotFather.
Obtain the bot's API Token.
For a group chat: Add the bot to the group and send a message. Use https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates to find the chat_id.
For a channel: Add the bot as an admin and use the channel`s username (e.g., @my_channel).
git clone https://github.com/your-repo/telegram-scheduled-bot.git
cd telegram-scheduled-bot
pip install python-telegram-bot schedule
Inside the tosend folder, place:
A text file named message.txt containing the message you want to send.
An image file named img.png (or any other image format supported by Telegram).
Create a settings.json file in the same directory as the script with the following content:
{
"folder_path": "tosend",
"message_file": "message.txt",
"image_file": "img.png",
"send_day": 15,
"send_hour": 10,
"send_minute": 0
}CHAT_ID: The ID of the chat or channel where the bot will send messages.
TOKEN: Your Telegram bot's API token.
folder_path - Path to the folder containing the message and image files.
message_file - Name of the text file containing the message (e.g., message.txt).
image_file - Name of the image file to be sent (e.g., img.png).
send_day - Day of the month to send the message (set to 0 for periodic posting).
send_hour - Hour of the day to send the message.
send_minute - Minute of the hour to send the message.
Set send_day to a specific day of the month (e.g., 15 for the 15th).
Example:
{
"send_day": 15,
"send_hour": 10,
"send_minute": 0
}The bot will send the message on the 15th of every month at 10:00 AM.
Set send_day to 0.
Specify the interval using send_hour and send_minute.
Example:
{
"send_day": 0,
"send_hour": 1,
"send_minute": 30
}The bot will send the message every 1 hour and 30 minutes.
Run the script using Python:
python telegram_bot.pyLogs are saved in a file named nowdate-telegrambot.log (e.g., 2023-10-15-telegrambot.log).
Each log entry includes a timestamp and a description of the action.
Logs are saved in a file named nowdate-telegrambot.log (e.g., 2023-10-15-telegrambot.log). Each log entry includes:
A timestamp in the format YYYY-MM-DD HH:MM:SS.
A description of the action (e.g., "Message and image sent successfully").
Example log:
[2023-10-15 10:00:00] Bot started. [2023-10-15 10:00:01] Message and image sent successfully.
Ensure the message.txt and img.png files exist in the tosend folder.
Double-check the token and chat_id in settings.json.
Ensure the bot has permission to send messages in the specified chat or channel.
Check the log file for detailed error messages.
This project is licensed under the MIT License. See the LICENSE file for details.
For questions or issues, please open an issue on the GitHub repository.
Enjoy automating your Telegram posts! 🚀