-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathecho_bot.py
47 lines (37 loc) · 1.36 KB
/
echo_bot.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
36
37
38
39
40
41
42
43
44
45
46
47
import logging
import argparse
from utils import setup_logging
from telegram.client import Telegram
"""
It answers "pong" if receives "ping"
Usage:
python examples/send_message.py api_id api_hash phone
"""
if __name__ == "__main__":
setup_logging(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument("api_id", help="API id") # https://my.telegram.org/apps
parser.add_argument("api_hash", help="API hash")
parser.add_argument("phone", help="Phone")
args = parser.parse_args()
tg = Telegram(
api_id=args.api_id,
api_hash=args.api_hash,
phone=args.phone,
database_encryption_key="changeme1234",
)
# you must call login method before others
tg.login()
def new_message_handler(update):
message_content = update["message"]["content"]
message_text = message_content.get("text", {}).get("text", "").lower()
is_outgoing = update["message"]["is_outgoing"]
if not is_outgoing and message_content["@type"] == "messageText" and message_text == "ping":
chat_id = update["message"]["chat_id"]
print(f"Ping has been received from {chat_id}")
tg.send_message(
chat_id=chat_id,
text="pong",
)
tg.add_message_handler(new_message_handler)
tg.idle() # blocking waiting for CTRL+C