This project is a Python automation script that fetches the latest tweet from a Twitter (X.com) profile and sends it to a specified Telegram channel using Selenium WebDriver. The script is designed to work with Microsoft Edge and automates the process of sending content from X.com to Telegram.
- Automates Microsoft Edge using Selenium to fetch the latest tweet from a Twitter profile.
- Strips specific unwanted words (e.g.,
عاجل |
) from the tweet. - Sends the tweet to a Telegram channel using Telegram Web.
- Ensures no duplicate tweets are sent by tracking the last sent tweet.
- Automatically retries every 2 minutes to fetch new tweets.
- Python 3.x
- Microsoft Edge and Edge WebDriver
- Selenium (
pip install selenium
)
-
Install the required Python libraries:
pip install selenium
-
Download Edge WebDriver:
- Make sure you have Microsoft Edge installed.
- Download the Edge WebDriver corresponding to your Edge version.
- Place it in a directory, e.g.,
C:/edgedriver/msedgedriver.exe
.
-
Modify the script:
- Update the
user_data_dir
andprofile_dir
paths in the script to match your Edge user profile. You can find the user data directory in the Edge settings underProfile
. - Change the
driver.get()
URLs to point to the correct X.com profile and Telegram channel.
user_data_dir = r'C:\Users\YOUR_USERNAME\AppData\Local\Microsoft\Edge\User Data' profile_dir = 'Profile 1'
- Update the
-
Run the script:
- Execute the script with Python:
python script.py
-
Initialization:
- The script launches Microsoft Edge with your specified profile to maintain login sessions for X.com and Telegram Web.
-
Fetch Tweet:
- The script navigates to the X.com profile of 20fourMedia (
https://x.com/20fourLive
). - It scrapes the latest tweet's text content and removes unwanted words such as
عاجل |
.
- The script navigates to the X.com profile of 20fourMedia (
-
Send to Telegram:
- The script navigates to your Telegram channel (
https://web.telegram.org/k/#@live20four
). - It locates the input box, clears it, and sends the tweet content using the Telegram Web interface.
- The script navigates to your Telegram channel (
-
Check for Duplicates:
- The script checks the latest tweet against the last sent tweet stored in
telegram_last_sent_tweet.txt
to avoid sending duplicates.
- The script checks the latest tweet against the last sent tweet stored in
-
Retry Mechanism:
- The script runs in an infinite loop, checking for new tweets every 2 minutes.
-
Headless Mode: To run the browser without a visible window, uncomment the following line in the script:
edge_options.add_argument("--headless")
-
Error Handling: The script includes basic error handling for elements not found or page load timeouts. Adjust the waiting times as necessary.
The script prints the latest tweet's content and logs successful Telegram posts in the terminal.
Latest Tweet: Breaking news...
Tweet sent successfully!