How to Use Telegram Auto Reply

As a man, there are many reasons why you must reply within 59 seconds, otherwise, something bad will happen.

Fortunately, Telegram provides an API.

  1. Obtain your developer token in this instead of a bot token.
  2. Install Telethon using pip3.
  3. RTFM to get Py working.

Telethon

1
pip3 install -U telethon --user

On Catalina, importing telethon will immediately abort without any other useful logs, and I spent the whole afternoon trying to solve this problem.

  • I tried switching Python versions with pyenv, but it didn’t work.
  • I ran it on Ubuntu, and it worked fine.
  • I finally found it here, OpenSSL was missing.
1
2
3
4
5
6
7
cd /usr/local/Cellar/openssl/1.0.2t/lib
sudo cp libssl.1.0.0.dylib libcrypto.1.0.0.dylib /usr/local/lib/
cd /usr/local/lib
mv libssl.dylib libssl_bak.dylib
mv libcrypto.dylib libcrypto_bak.dylib
sudo ln -s libssl.1.0.0.dylib libssl.dylib
sudo ln -s libcrypto.1.0.0.dylib libcrypto.dylib

Auto Reply

replyHer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from telethon import TelegramClient, events
import time

# Use your own values from my.telegram.org
api_id = 123456
api_hash = 'xxxx'
phone = '123456789'

client = TelegramClient('session', api_id, api_hash)


greetings = ['ok', 'cool', '😂', 'em']

@client.on(events.NewMessage)
async def handle_new_message(event):

from_user = await event.client.get_entity(event.from_id)
if from_user.phone == phone:
print(time.asctime(), '-', event.message)
# tested on a real girl, she figured it out at the second reply
# so randomly choose to reply within 5 - 59 seconds
await asyncio.sleep(random.randrange(5, 59))
if random.choice([True, False]):
i, s = random.randrange(2, 5), random.choice(greetings)
# typing 2 - 5 seconds
async with client.action(phone, 'typing'):
await asyncio.sleep(i)
await client.send_message(phone, s)


print(time.asctime(), '-', 'Auto-replying...')
client.start()
client.run_until_disconnected()
print(time.asctime(), '-', 'Stopped!')

Scheduled Messages

spamHer.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from telethon import TelegramClient, events
import time

api_id = 123456
api_hash = 'xxxx'
phone = '123456789'

client = TelegramClient('session', api_id, api_hash)
greetings = ['Yo!', 'Hi', 'Hello', 'How have you been?', 'How are you?', 'What\'s up today?', 'How are you doing?', 'How\’s it going?']


async def asking(s):
await client.send_message(phone, s)

hour = 60*60

async def main():
while True:
await asking(random.choice(annoyingStrings))
i = random.randrange(12*hour, 24*hour)
time.sleep(i)

with client:
client.loop.run_until_complete(main())

Anyway, the functionality of the list template is not enough to keep the conversation going for more than 1 minute.

Her.msg -> telethon -> AI bot
Her <- telethon <- bot.msg

It may be a good solution 🌚🌚🌚

Translated by gpt-3.5-turbo