Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
software-project-WS25
Telegram Team
Commits
5fefa710
You need to sign in or sign up before continuing.
Commit
5fefa710
authored
Nov 12, 2025
by
Pranav
Browse files
Minor enhancements and bug fixes
parent
d3c2b97d
Pipeline
#12135
failed with stages
in 9 seconds
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Desktop/Telegram_Bot/services/telegram_listener.py
View file @
5fefa710
...
...
@@ -47,7 +47,7 @@ async def list_joined_channels(client):
return
channels
async
def
process_
last_
messages_for_all
(
client
,
process_message
,
limit
):
async
def
process_messages_for_all
_channels
(
client
,
process_message
,
limit
):
channels
=
await
list_joined_channels
(
client
)
print
(
f
"No INVITE_URL provided. Listening to all chats. Found
{
len
(
channels
)
}
channels."
)
...
...
@@ -65,12 +65,17 @@ async def process_last_messages_for_all(client, process_message, limit):
async
def
handle_all
(
event
):
await
process_message
(
event
.
message
)
@
client
.
on
(
events
.
MessageEdited
())
async
def
handle_edit
(
event
):
message
=
event
.
message
await
process_message
(
message
)
return
channels
# optional, if you need the list
async
def
process_
last_
message_for_one_channel
(
client
,
process_message
,
target
,
limit
):
async
def
process_message
s
_for_one_channel
(
client
,
process_message
,
target
,
limit
):
msgs
=
[
msg
async
for
msg
in
client
.
iter_messages
(
target
,
limit
=
10
)]
msgs
=
[
msg
async
for
msg
in
client
.
iter_messages
(
target
,
limit
=
limit
)]
for
m
in
reversed
(
msgs
):
await
process_message
(
m
)
...
...
@@ -143,7 +148,7 @@ def write_text_file_per_post(message,sender_id):
except
Exception
as
e
:
print
(
f
"Error writing text file for post
{
message
.
id
}
:
{
e
}
"
)
async
def
process_message_for_
csv
(
message
):
async
def
process_message_for_
json
(
message
):
sentiment
=
await
asyncio
.
to_thread
(
sentiment_analyzer
,
message
.
text
or
""
)
sender_id
=
getattr
(
message
,
"sender_id"
,
None
)
row
=
{
...
...
@@ -206,14 +211,7 @@ async def ensure_joined(client, invite_url: str):
pass
return
entity
async
def
main
():
client
=
TelegramClient
(
SESSION
,
API_ID
,
API_HASH
)
await
client
.
start
()
# Resolve and ensure we are re in the channel/group
target
=
await
ensure_joined
(
client
,
INVITE_LINK
)
async
def
process_message
(
message
):
async
def
process_message
(
message
):
# 1. Basic Info
print
(
"--- New Message ---"
)
...
...
@@ -242,15 +240,22 @@ async def main():
print
(
f
" -
{
label
}
:
{
reaction_count
.
count
}
"
)
print
(
"------
\n
"
)
await
process_message_for_csv
(
message
)
await
process_message_for_json
(
message
)
async
def
main
():
client
=
TelegramClient
(
SESSION
,
API_ID
,
API_HASH
)
await
client
.
start
()
# Resolve and ensure we are re in the channel/group
target
=
await
ensure_joined
(
client
,
INVITE_LINK
)
if
target
:
print
(
"Listening to single channel "
)
process_
last_
message_for_one_channel
(
client
,
process_message
,
target
,
limit
=
5
)
await
process_message
s
_for_one_channel
(
client
,
process_message
,
target
,
limit
=
5
)
else
:
print
(
"No INVITE_URL provided. Listening to all chats."
)
await
process_
last_
messages_for_all
(
client
,
process_message
,
limit
=
5
)
await
process_messages_for_all
_channels
(
client
,
process_message
,
limit
=
5
)
print
(
"Listening for messages..."
)
await
client
.
run_until_disconnected
()
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment