Skip to content

Instantly share code, notes, and snippets.

@VycktorStark
Created August 13, 2020 15:30
Show Gist options
  • Save VycktorStark/b27257a6dade1a8a26c3747c1ee030c7 to your computer and use it in GitHub Desktop.
Save VycktorStark/b27257a6dade1a8a26c3747c1ee030c7 to your computer and use it in GitHub Desktop.
Notify actions for when a user joins or leaves the group
__author__ = "Vycktor Stark"
import json, requests, re, time
SECRET_KEY = '523403928:ARxEXsjjhdosifoTm_QKPR_hy-EOcY0Qi4' # Define your bot's token
TELEGRAM_API = f'https://api.telegram.org/bot{SECRET_KEY}'
forward_chat_information = 438131290 # id to forward chat information
def polling():
temp = 100
while True:
data = requests.get(f"{TELEGRAM_API}/getUpdates", params=dict(offset=temp, timeout=int(temp+1), allowd_updates='message'), headers={"Content-Type": "application/json"})
data = dict(data.json())
if len(data["result"]) == 0:
resp = json.dumps(dict(ping='pong'))
elif ("result" in data):
temp = int(data['result'][0]['update_id'] + 1)
resp = data['result'][0]
handler(resp)
def ident(msg, params):
try:
user = msg['first_name']
if ("last_name" in msg): user = f"{user} {msg['last_name']}"
if ('username' in msg):
params['parse_mode'] = 'HTML'
user = f"<a href='https://t.me/{msg['username']}'>{user}</a>"
user = f"{user} ({msg['from']['id']})"
except Exception as error:
user = "404 - Not Found"
return user
def handler(vetor):
print(vetor)
if ("message" in vetor):
msg = vetor["message"]
params = dict(chat_id = forward_chat_information, text = 'PONG')
if (("left_chat_member" or "left_chat_members") in msg):
params['text'] = f'User {ident(msg['left_chat_member'], params)} left chat'
if (("new_chat_participant" or "new_chat_member" or "new_chat_members") in msg):
params['text'] = f'User {ident(msg['new_chat_member'], params)} join chat'
requests.post(f"{TELEGRAM_API}/sendMessage", params=params, headers={'Content-Type':'application/json'})
if __name__ == '__main__':
try:
print("\nstarting bot")
polling()
except KeyboardInterrupt:
print("\nending bot")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment