1+ # This bot is needed to connect two people and their subsequent anonymous communication
2+ #
3+ # Avaiable commands:
4+ # `/start` - Just send you a messsage how to start
5+ # `/find` - Find a person you can contact
6+ # `/stop` - Stop active conversation
7+
8+ import telebot
9+ from telebot import types
10+
11+ # Initialize bot with your token
12+ bot = telebot .TeleBot (TOKEN )
13+
14+ # The `users` variable is needed to contain chat ids that are either in the search or in the active dialog, like {chat_id, chat_id}
15+ users = {}
16+ # The `freeid` variable is needed to contain chat id, that want to start conversation
17+ # Or, in other words: chat id of user in the search
18+ freeid = None
19+
20+ # `/start` command handler
21+ #
22+ # That command only sends you 'Just use /find command!'
23+ #
24+ @bot .message_handler (commands = ['start' ])
25+ def start (message : types .Message ):
26+ bot .send_message (message .chat .id , 'Just use /find command!' )
27+
28+ # `/find` command handler
29+ #
30+ # That command finds opponent for you
31+ #
32+ # That command according to the following principle:
33+ # 1. You have written `/find` command
34+ # 2. If you are already in the search or have an active dialog, bot sends you 'Shut up!'
35+ # 3. If not:
36+ # 3.1. Bot sends you 'Finding...'
37+ # 3.2. If there is no user in the search:
38+ # 3.2.2. `freeid` updated with `your_chat_id`
39+ # 3.3. If there is user in the search:
40+ # 3.3.1. Both you and the user in the search recieve the message 'Founded!'
41+ # 3.3.2. `users` updated with a {user_in_the_search_chat_id, your_chat_id}
42+ # 3.3.3. `users` updated with a {your_chat_id, user_in_the_search_id}
43+ # 3.3.4. `freeid` updated with `None`
44+ #
45+ @bot .message_handler (commands = ['find' ])
46+ def find (message : types .Message ):
47+ global freeid
48+
49+ if message .chat .id not in users :
50+ bot .send_message (message .chat .id , 'Finding...' )
51+
52+ if freeid == None :
53+ freeid = message .chat .id
54+ else :
55+ # Question:
56+ # Is there any way to simplify this like `bot.send_message([message.chat.id, freeid], 'Founded!')`?
57+ bot .send_message (message .chat .id , 'Founded!' )
58+ bot .send_message (freeid , 'Founded!' )
59+
60+ users [freeid ] = message .chat .id
61+ users [message .chat .id ] = freeid
62+ freeid = None
63+
64+ print (users , freeid ) # Debug purpose, you can remove that line
65+ else :
66+ bot .send_message (message .chat .id , 'Shut up!' )
67+
68+ # `/stop` command handler
69+ #
70+ # That command stops your current conversation (if it exist)
71+ #
72+ # That command according to the following principle:
73+ # 1. You have written `/stop` command
74+ # 2. If you are not have active dialog or you are not in search, bot sends you 'You are not in search!'
75+ # 3. If you are in active dialog:
76+ # 3.1. Bot sends you 'Stopping...'
77+ # 3.2. Bot sends 'Your opponent is leavin`...' to your opponent
78+ # 3.3. {your_opponent_chat_id, your_chat_id} removes from `users`
79+ # 3.4. {your_chat_id, your_opponent_chat_id} removes from `users`
80+ # 4. If you are only in search:
81+ # 4.1. Bot sends you 'Stopping...'
82+ # 4.2. `freeid` updated with `None`
83+ #
84+ @bot .message_handler (commands = ['stop' ])
85+ def stop (message : types .Message ):
86+ global freeid
87+
88+ if message .chat .id in users :
89+ bot .send_message (message .chat .id , 'Stopping...' )
90+ bot .send_message (users [message .chat .id ], 'Your opponent is leavin`...' )
91+
92+ del users [users [message .chat .id ]]
93+ del users [message .chat .id ]
94+
95+ print (users , freeid ) # Debug purpose, you can remove that line
96+ elif message .chat .id == freeid :
97+ bot .send_message (message .chat .id , 'Stopping...' )
98+ freeid = None
99+
100+ print (users , freeid ) # Debug purpose, you can remove that line
101+ else :
102+ bot .send_message (message .chat .id , 'You are not in search!' )
103+
104+ # message handler for conversation
105+ #
106+ # That handler needed to send message from one opponent to another
107+ # If you are not in `users`, you will recieve a message 'No one can hear you...'
108+ # Otherwise all your messages are sent to your opponent
109+ #
110+ # Questions:
111+ # Is there any way to improve readability like `content_types=['all']`?
112+ # Is there any way to improve "elif-zone"? Like:
113+ # `bot.send_message(users[message.chat.id], message.data)`
114+ #
115+ @bot .message_handler (content_types = ['animation' , 'audio' , 'contact' , 'dice' , 'document' , 'location' , 'photo' , 'sticker' , 'text' , 'venue' , 'video' , 'video_note' , 'voice' ])
116+ def chatting (message : types .Message ):
117+ if message .chat .id in users and users [message .chat .id ] != None :
118+
119+ if message .content_type == 'animation' :
120+ bot .send_animation (users [message .chat .id ], message .animation .file_id )
121+ elif message .content_type == 'audio' :
122+ bot .send_audio (users [message .chat .id ], message .audio .file_id )
123+ elif message .content_type == 'contact' :
124+ bot .send_contact (users [message .chat .id ], message .contact .phone_number , message .contact .first_name )
125+ elif message .content_type == 'dice' :
126+ bot .send_dice (users [message .chat .id ], message .dice .emoji )
127+ elif message .content_type == 'document' :
128+ bot .send_document (users [message .chat .id ], message .document .file_id )
129+ elif message .content_type == 'location' :
130+ bot .send_location (users [message .chat .id ], message .location .latitude , message .location .longitude )
131+ elif message .content_type == 'photo' :
132+ bot .send_photo (users [message .chat .id ], message .photo )
133+ elif message .content_type == 'sticker' :
134+ bot .send_sticker (users [message .chat .id ], message .sticker .file_id )
135+ elif message .content_type == 'text' :
136+ bot .send_message (users [message .chat .id ], message .text )
137+ elif message .content_type == 'venue' :
138+ bot .send_venue (users [message .chat .id ], message .venue .location .latitude , message .venue .location .longitude , message .venue .title , message .venue .address )
139+ elif message .content_type == 'video' :
140+ bot .send_video (users [message .chat .id ], message .video .file_id )
141+ elif message .content_type == 'video_note' :
142+ bot .send_video_note (users [message .chat .id ], message .video_note .file_id )
143+ elif message .content_type == 'voice' :
144+ bot .send_voice (users [message .chat .id ], message .voice .file_id )
145+
146+ else :
147+ bot .send_message (message .chat .id , 'No one can hear you...' )
148+
149+ # Start retrieving updates
150+ bot .polling ()
0 commit comments