Advertisement
Alex-Flexer

Untitled

Mar 18th, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 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_id = 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_id
  13.  
  14. updates = get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()
  15. last_message_id = len(updates['result'])
  16. amount_new_messages = last_message_id - previous_message_id
  17. previous_message_id = last_message_id
  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.  
  26. def send_bay_elephant() -> None:
  27.  
  28. while True:
  29. sleep(2)
  30. users_id = get_all_users_id_of_new_messages()
  31.  
  32. for user_id in users_id:
  33. post(API_URL_TEMPLATE.format(token=MY_TOKEN, method='sendMessage'),
  34. data={'chat_id': user_id, 'text': f'Купи слона!{users_id}'})
  35.  
  36. send_bay_elephant()
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement