Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from requests import get, post
- from time import sleep
- MY_TOKEN = "6229457774:AAHXcF7RwLmAVvLTNy6Owk8xvuY8h2A2TUU"
- API_URL_TEMPLATE = "https://api.telegram.org/bot{token}/{method}"
- previous_message_id = len(get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()['result'])
- def get_all_users_id_of_new_messages() -> set:
- global previous_message_id
- updates = get(API_URL_TEMPLATE.format(token=MY_TOKEN, method='getUpdates')).json()
- last_message_id = len(updates['result'])
- amount_new_messages = last_message_id - previous_message_id
- previous_message_id = last_message_id
- if amount_new_messages == 0:
- return set()
- return set(message['message']['from']['id'] for message in updates['result'][-amount_new_messages:])
- def send_bay_elephant() -> None:
- while True:
- sleep(2)
- users_id = get_all_users_id_of_new_messages()
- for user_id in users_id:
- post(API_URL_TEMPLATE.format(token=MY_TOKEN, method='sendMessage'),
- data={'chat_id': user_id, 'text': f'Купи слона!{users_id}'})
- send_bay_elephant()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement