Created
December 9, 2022 15:49
-
-
Save giacomoran/42d5d866797a65170f9b54eeab69d826 to your computer and use it in GitHub Desktop.
Telegram bot that sends you a message when a Twitter handle is available
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Refs: | |
# - ChatGPT ;) | |
# - https://medium.com/codex/using-python-to-send-telegram-messages-in-3-simple-steps-419a8b5e5e2 | |
import time | |
import requests | |
# First find out your Telegram chat id: | |
# - create a bot with @BotFather | |
# - send a message to your bot | |
# - get the chat id by uncommenting the code below | |
TOKEN_TELEGRAM = "..." | |
ID_TELEGRAM_CHAT = "..." | |
# url_telegram = f"https://api.telegram.org/bot{TOKEN_TELEGRAM}/getUpdates" | |
# print(requests.get(url_telegram).json()) | |
def send_telegram_message(message): | |
print(f"Sending Telegram message: {message}") | |
requests.get( | |
f"https://api.telegram.org/bot{TOKEN_TELEGRAM}/sendMessage?chat_id={ID_TELEGRAM_CHAT}&text={message}") | |
usernames = ["handle1", "handle2"] | |
url_base = "https://twitter.com/i/api/i/users/username_available.json" | |
params_base = {"full_name": "Your Twitter full name", "suggest": "false"} | |
# How to get your request headers: | |
# - Navigate to twitter.com -> Settings -> Your account -> Change username | |
# - Open Chrome's network tab and clean | |
# - type a new username, a request should appear, copy the three headers below | |
headers = { | |
"authorization": "...", | |
"cookie": '...', | |
"x-csrf-token": "...", | |
} | |
def check(): | |
for username in usernames: | |
params = {**params_base, "username": username} | |
response = requests.get(url_base, headers=headers, params=params) | |
if (response.status_code != 200): | |
send_telegram_message( | |
f"Error: {response.status_code}, stopping the script.") | |
return True | |
if response.json()["valid"]: | |
send_telegram_message(f"Username {username} is available!") | |
usernames.remove(username) | |
break | |
return False | |
i = 0 | |
while len(usernames) > 0: | |
stop = check() | |
if stop: | |
break | |
i = i + 1 | |
if i % 60 == 0: | |
print(f"Checked {i} times") | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment