如何使用 Telegram 自动回复

作为男人,有很多原因必须在 59 秒内回复,否则,会发生一些不好的事情。

幸运的是,Telegram 提供了一个 API。

  1. 此处中获取您的开发者令牌,而不是机器人令牌
  2. 用安装 telethon pip3
  3. RTFM 使 py 起作用

Telethon

1
pip3 install -U telethon --user

在 Catalina 上,import telethon导入后将立即中止,而没有任何其他有用的日志,这使我整个下午都无法解决该问题

  • 我试图用 pyenv 切换 python 版本,不起作用
  • 在 Ubuntu 上运行它,正常工作
  • 终于在这里找到, Python 在 macOS 10.15 上崩溃, OpenSSL 失踪
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!')

预定讯息

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())

无论如何,列表模板的功能不足以使对话持续超过 1 分钟

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

可能是一个很好的解决方案 🌚🌚🌚