Skip to content

Instantly share code, notes, and snippets.

@vit1-irk
Created July 8, 2019 09:24
Show Gist options
  • Save vit1-irk/a2e3a837203e2d72fe6a755706ac4446 to your computer and use it in GitHub Desktop.
Save vit1-irk/a2e3a837203e2d72fe6a755706ac4446 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from py_tdlib import Client, Pointer, Auth
from py_tdlib.constructors import (
getMe, viewMessages, getChat, getChats
)
full_ignore = [] # integer ids of groups and chats you want to fully mark as read
stop_keywords = ["programming", "linux"] # never mark chat as read, if there are such keywords in it
stop_keywords = [word.lower() for word in stop_keywords]
unread_history = {}
# demo api key, get your from https://my.telegram.org
#api_id = 94575
#api_hash = "a3406de8d171bb422bb6ddf3bbd800e2"
# creating a client
tdjson = Pointer("./libtdjson.so")
client = Client(tdjson)
# login using phone number
tdjson.verbosity(0)
Auth(api_id, api_hash, client).phone()
# making an API call
result = client.send(getMe())
print(result)
result = client.send(getChats(9223372036854775807, None, 100)).to_dict()
for id in result["chat_ids"]:
chat_info = client.send(getChat(id)).to_dict()
print(str(id) + " - " + chat_info["title"])
# get updates
for update in client.get_updates():
update = update.to_dict()
if update["@type"] == "updateChatReadInbox":
unread_history[str(update["chat_id"])] = 0
if update["@type"] in ["updateNewMessage", "updateChatNewMessage"]:
msg = update["message"]
print(msg["chat_id"])
print(msg["content"])
if not str(msg["chat_id"]) in unread_history.keys():
unread_history[str(msg["chat_id"])] = 0
if msg["is_outgoing"]:
continue
if msg["contains_unread_mention"]:
unread_history[str(msg["chat_id"])] += 1
continue
_type = msg["content"]["@type"]
if _type == "messageText":
msgtext = msg["content"]["text"]["text"].lower()
cycle_pass = False
for word in stop_keywords:
if word in msgtext:
cycle_pass = True
if cycle_pass:
unread_history[str(msg["chat_id"])] += 1
continue
if msg["chat_id"] in full_ignore or _type in ["messageAnimation", "messageSticker"]:
print("ignoring the message")
if unread_history[str(msg["chat_id"])] != 0:
continue
result = client.send(viewMessages(chat_id = msg["chat_id"], message_ids = [msg["id"]], force_read = True))
unread_history[str(msg["chat_id"])] = 0
# messageText -> text -> text
# messageAnimation
# messageSticker
# messagePhoto
if _type == "messageDocument":
doc = msg["content"]["document"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment