Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. import requests
  2. from dotenv import load_dotenv
  3. import os
  4.  
  5. load_dotenv()
  6. TOKEN = os.getenv('TOKEN')
  7. BASE_URL = f"https://api.telegram.org/bot{TOKEN}"
  8.  
  9. reply = requests.get(f'{BASE_URL}/getupdates')
  10.  
  11. for data in reply.json().get('result'):
  12.     try:
  13.         # Hole den Nachrichtentext, den Vornamen und die ChatID
  14.         message = str(data["message"]["text"])
  15.         first_name = data["message"]["chat"]["first_name"]
  16.         chat_id = data["message"]["chat"]["id"]
  17.  
  18.         response = {"chat_id": chat_id, }
  19.  
  20.         # Reagiere auf die Eingabe
  21.         # Fallunterscheidung
  22.         if message.startswith('/start'):
  23.             response['text'] = f"Hello {first_name}".encode("utf8")
  24.             requests.post(f"{BASE_URL}/sendMessage", response)
  25.  
  26.         # Hier können jetzt noch weitere Kommandos eingetragen werden
  27.  
  28.         elif message.startswith('/cat'):
  29.             response['photo'] = f'https://cataas.com/cat/says/Hello%20{first_name}'
  30.             requests.post(f"{BASE_URL}/sendPhoto", response)
  31.  
  32.         elif message.startswith('/dog'):
  33.             contents = requests.get('https://random.dog/woof.json').json()
  34.             response['photo'] = contents['url']
  35.             requests.post(f"{BASE_URL}/sendPhoto", response)
  36.  
  37.         elif message.startswith('/chatid'):
  38.             response['text'] = f"Chat ID: {chat_id}".encode("utf8")
  39.             requests.post(f"{BASE_URL}/sendMessage", response)
  40.  
  41.         else:
  42.             response['text'] = f"Please /start, {first_name}".encode("utf8")
  43.             requests.post(f"{BASE_URL}/sendMessage", response)
  44.  
  45.     except Exception as e:
  46.         print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement