Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from dotenv import load_dotenv
- import os
- load_dotenv()
- TOKEN = os.getenv('TOKEN')
- BASE_URL = f"https://api.telegram.org/bot{TOKEN}"
- reply = requests.get(f'{BASE_URL}/getupdates')
- for data in reply.json().get('result'):
- try:
- # Hole den Nachrichtentext, den Vornamen und die ChatID
- message = str(data["message"]["text"])
- first_name = data["message"]["chat"]["first_name"]
- chat_id = data["message"]["chat"]["id"]
- response = {"chat_id": chat_id, }
- # Reagiere auf die Eingabe
- # Fallunterscheidung
- if message.startswith('/start'):
- response['text'] = f"Hello {first_name}".encode("utf8")
- requests.post(f"{BASE_URL}/sendMessage", response)
- # Hier können jetzt noch weitere Kommandos eingetragen werden
- elif message.startswith('/cat'):
- response['photo'] = f'https://cataas.com/cat/says/Hello%20{first_name}'
- requests.post(f"{BASE_URL}/sendPhoto", response)
- elif message.startswith('/dog'):
- contents = requests.get('https://random.dog/woof.json').json()
- response['photo'] = contents['url']
- requests.post(f"{BASE_URL}/sendPhoto", response)
- elif message.startswith('/chatid'):
- response['text'] = f"Chat ID: {chat_id}".encode("utf8")
- requests.post(f"{BASE_URL}/sendMessage", response)
- else:
- response['text'] = f"Please /start, {first_name}".encode("utf8")
- requests.post(f"{BASE_URL}/sendMessage", response)
- except Exception as e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement