Advertisement
Alex-Flexer

Untitled

Mar 18th, 2023 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. from requests import get, post
  2. from time import sleep
  3.  
  4.  
  5. MY_TOKEN = "6229457774:AAHXcF7RwLmAVvLTNy6Owk8xvuY8h2A2TUU"
  6. API_URL_TEMPLATE = "https://api.telegram.org/bot{token}/{method}"
  7.  
  8. previous_message_amount = len(get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()['result'])
  9.  
  10.  
  11. def get_all_users_id_of_new_messages() -> set:
  12. global previous_message_amount
  13.  
  14. updates = get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()
  15. last_message_amount = len(updates['result'])
  16. amount_new_messages = last_message_amount - previous_message_amount
  17. previous_message_amount = last_message_amount
  18.  
  19. if amount_new_messages == 0:
  20. return set()
  21.  
  22. return set(message['message']['from']['id'] for message in updates['result'][-amount_new_messages:])
  23.  
  24.  
  25. def send_bay_elephant() -> None:
  26.  
  27. while True:
  28. sleep(2)
  29. users_id = get_all_users_id_of_new_messages()
  30.  
  31. for user_id in users_id:
  32. post(API_URL_TEMPLATE.format(token=MY_TOKEN, method='sendMessage'),
  33. data={'chat_id': user_id, 'text': 'Купи слона!'})
  34.  
  35. send_bay_elephant()
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement