Skip to content

Instantly share code, notes, and snippets.

@Amar1729
Created January 26, 2021 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Amar1729/93e57121b1bdc9dade5a7e759952a9bf to your computer and use it in GitHub Desktop.
Save Amar1729/93e57121b1bdc9dade5a7e759952a9bf to your computer and use it in GitHub Desktop.
use telethon to send a bunch of messages (replies to polls) to a telegram chat
#! /usr/bin/env python3
import datetime
import json
from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerChat, MessageMediaPoll
# expects a settings.py in the same directory with these vars defined
from settings import API_ID, API_HASH, CHAT_ID
client = TelegramClient('session', API_ID, API_HASH)
client.start()
chat = InputPeerChat(CHAT_ID)
RESULTS = "results.json"
def get_next_sunday(d):
while d.strftime("%a") != "Sun":
d = d + datetime.timedelta(days=1)
return d.strftime("%m/%d/%y")
def get_movie_polls():
# hardcoded: we started doing movie polls in august 2020
beginning_date = datetime.datetime(year=2020, month=8, day=1, tzinfo=datetime.timezone.utc)
# hardcoded: this is the ID of the first poll i replied to (16.12.20)
max_id = 274101
for msg in client.iter_messages(chat, max_id=max_id):
if msg.media and isinstance(msg.media, MessageMediaPoll):
yield msg
if msg.date < beginning_date:
break
def interactive_ask():
"""For all the polls in the proper date range, print them nicely
and ask whether or not we want to reply to them.
Their IDs will be saved into a serialized list, which we then can
read and send replies to.
"""
polls = []
user_ids = {}
for msg in get_movie_polls():
print(f"msg.id: {msg.id}")
print(f"msg.from_id: {msg.from_id}")
print(msg.media.poll.stringify())
answer = input("Add poll to movie polls? [y/n]: ")
if answer == "y":
polls.append(msg.id)
uid = msg.from_id.user_id
if uid not in user_ids:
first_name = msg.get_sender().first_name
user_ids[uid] = first_name
results = {
"polls": polls,
"uids": user_ids,
}
with open(RESULTS, "w") as f:
json.dump(results, f)
def interactive_reply():
"""Reply to each poll with text such as:
Amar's #movie pick day/month/year
(or, for october: horror #movie day/month/year)
Show the poll to the user before replying, to make sure it's one that you're not about to duplicate.
"""
with open(RESULTS) as f:
j = json.load(f)
user_ids = j["uids"]
for msg_id in j["polls"][::-1]:
msg = client.get_messages(chat, ids=msg_id)
first_name = user_ids[f"{msg.from_id.user_id}"]
print(msg.id)
print(msg.date)
print(first_name)
print(msg.media.poll.stringify())
answer = input("Reply to this poll? (y for yes, h for horror) [y/h/n]: ")
if answer in "yh":
d = get_next_sunday(msg.date)
if answer == "y":
message = f"{first_name}'s #movie choice {d}"
else:
message = f"horror #movie choice {d}"
# print(message)
# comment out reply() if you want to test the messages before sending replies to your chat
msg.reply(message)
print()
if __name__ == "__main__":
# interactive_ask()
interactive_reply()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment