Certainly! Below is a Python script that accomplishes the task you've described:
- It reads elements like
|03|2026|113 from a cell (or multiple cells) in an Excel file.
- It adds
.chk at the beginning of each element.
- It sends the modified elements to a private Telegram group.
Before running the script, make sure to:
- Install the required Python libraries.
- Create a Telegram bot and obtain its token.
- Get the chat ID of your private Telegram group.
- Ensure that your bot is added to the group.
Here's the step-by-step guide and the script:
import pandas as pd
import requests
# Step 1: Define the Excel file path
excel_file = 'data.xlsx' # Replace with your Excel file name
# Step 2: Read the Excel file
# Assuming the data is in the first sheet and first column
df = pd.read_excel(excel_file)
# Extract the data from the first column, dropping any empty cells
data_column = df.iloc[:, 0].dropna()
# Step 3: Process the data by adding '.chk' at the beginning of each element
modified_values = [".chk" + str(value) for value in data_column]
# Step 4: Set up Telegram bot credentials
bot_token = 'YOUR_TELEGRAM_BOT_TOKEN' # Replace with your bot's token
chat_id = 'YOUR_TELEGRAM_GROUP_CHAT_ID' # Replace with your group's chat ID
# Telegram API endpoint
url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
# Step 5: Send the modified elements to the Telegram group
for modified_value in modified_values:
# Prepare the message parameters
params = {
'chat_id': chat_id,
'text': modified_value
}
# Send the message
response = requests.post(url, data=params)
# Check if the message was sent successfully
if response.status_code == 200:
print(f'Message sent successfully: {modified_value}')
else:
print(f'Failed to send message: {modified_value}')
print('Error:', response.text)
Detailed Instructions
1. Install Required Libraries
You need the pandas and requests libraries to run this script. Install them using:
pip install pandas requests openpyxl
pandas: For reading Excel files.
requests: For making HTTP requests to the Telegram API.
openpyxl: Needed by pandas to read .xlsx files.
2. Prepare Your Excel File
Ensure your Excel file (data.xlsx) is formatted correctly:
- Place all elements like
|03|2026|113 in the first column (Column A) of the first sheet.
- Remove any unnecessary data from other columns or rows.
3. Set Up a Telegram Bot
Create a Bot and Get the Token
-
Open Telegram and search for @BotFather.
-
Start a chat and send /newbot.
-
Follow the prompts to name your bot and get the token.
You'll receive a message like:
Done! Congratulations on your new bot. You will find it at telegram.me/YourBotName.
Use this token to access the HTTP API:
123456789:ABCdefGhIJKlmNoPQRsTuVWXyZ
-
Copy the token and replace 'YOUR_TELEGRAM_BOT_TOKEN' in the script with it.
Add the Bot to Your Telegram Group
- Create a new group or use an existing one.
- Add your bot to the group.
- Promote the bot to an administrator if necessary (depending on group settings).
4. Get Your Telegram Group Chat ID
To send messages to the group, you need the group's chat ID.
Method 1: Using @myidbot
- Add
@myidbot to your group.
- Send
/getgroupid in the group.
- The bot will reply with the group's chat ID.
Method 2: Using getUpdates
-
Send a message in your group.
-
Access the following URL in your browser (replace YOUR_TELEGRAM_BOT_TOKEN):
https://api.telegram.org/botYOUR_TELEGRAM_BOT_TOKEN/getUpdates
-
Look for the chat section in the latest messages and find the id field.
Example:
"chat": {
"id": -1234567890123,
"title": "Your Group Name",
"type": "group"
}
-
Copy the id value and replace 'YOUR_TELEGRAM_GROUP_CHAT_ID' in the script with it.
Note: Group chat IDs are negative numbers.
5. Run the Script
Save the script to a file, for example, send_to_telegram.py, and run it:
python send_to_telegram.py
6. Verify Messages in Telegram
Check your Telegram group to see if the messages have been sent successfully.
Notes and Troubleshooting
- Data Formatting: Ensure that the elements in your Excel file are correctly formatted and there are no unintended spaces or characters.
- Bot Permissions: If messages aren't sending, make sure your bot has permission to send messages in the group.
- Error Handling: The script includes basic error handling. If a message fails to send, it will print the error response from Telegram.
- Multiple Cells: The script reads all non-empty cells from the first column. You can adjust the column index (
df.iloc[:, 0]) to change the column.
Customizations
-
Specific Cells: If you need to read from a specific cell or range, modify the DataFrame indexing accordingly.
-
Message Formatting: You can enhance the message content by adding additional text or formatting.
-
Sending a Single Message: To send all modified elements in a single message, concatenate them:
message = '\n'.join(modified_values)
params = {
'chat_id': chat_id,
'text': message
}
response = requests.post(url, data=params)
Security Considerations
- Protect Your Token: Never share your bot token publicly. It's sensitive information that allows control over your bot.
- Secure Storage: Consider storing your bot token and chat ID in environment variables or a separate configuration file that's not checked into version control.
Feel free to ask if you need further assistance or modifications to the script!