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
edc84e44
Commit
edc84e44
authored
Dec 16, 2025
by
Pranav
Browse files
Minor changes
parent
c7369c33
Pipeline
#12304
passed with stages
in 34 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Desktop/Telegram_Bot/services/telegram_listener.py
View file @
edc84e44
...
@@ -23,6 +23,7 @@ API_ID = int(os.getenv("API_ID"))
...
@@ -23,6 +23,7 @@ API_ID = int(os.getenv("API_ID"))
API_HASH
=
os
.
getenv
(
"API_HASH"
)
API_HASH
=
os
.
getenv
(
"API_HASH"
)
SESSION
=
os
.
getenv
(
"SESSION"
)
SESSION
=
os
.
getenv
(
"SESSION"
)
INVITE_LINK
=
os
.
getenv
(
"INVITE_LINK"
)
INVITE_LINK
=
os
.
getenv
(
"INVITE_LINK"
)
LIMIT
=
os
.
getenv
(
"LIMIT"
);
DATA_DIR
=
Path
(
os
.
getenv
(
"DATA_DIR"
,
"/app/data"
))
DATA_DIR
=
Path
(
os
.
getenv
(
"DATA_DIR"
,
"/app/data"
))
OUTPUT_DIR
=
DATA_DIR
/
"output/telegram"
OUTPUT_DIR
=
DATA_DIR
/
"output/telegram"
...
@@ -198,10 +199,15 @@ async def ensure_joined(client, invite_url):
...
@@ -198,10 +199,15 @@ async def ensure_joined(client, invite_url):
# ======================================================
# ======================================================
async
def
process_single_channel
(
client
,
target
,
limit
):
async
def
process_single_channel
(
client
,
target
,
limit
):
print
(
"Processing Historical Messages"
)
history
=
[
m
async
for
m
in
client
.
iter_messages
(
target
,
limit
=
limit
)]
history
=
[
m
async
for
m
in
client
.
iter_messages
(
target
,
limit
=
limit
)]
for
m
in
reversed
(
history
):
for
m
in
reversed
(
history
):
await
process_message_for_storage
(
m
)
await
process_message_for_storage
(
m
)
print
(
"Messages fetched and stored in file!!"
)
print
(
"Listening for real time messages"
)
@
client
.
on
(
events
.
NewMessage
(
chats
=
target
))
@
client
.
on
(
events
.
NewMessage
(
chats
=
target
))
async
def
on_new
(
event
):
async
def
on_new
(
event
):
await
process_message_for_storage
(
event
.
message
)
await
process_message_for_storage
(
event
.
message
)
...
@@ -211,15 +217,21 @@ async def process_single_channel(client, target, limit):
...
@@ -211,15 +217,21 @@ async def process_single_channel(client, target, limit):
await
process_message_for_storage
(
event
.
message
)
await
process_message_for_storage
(
event
.
message
)
async
def
process_all_channels
(
client
,
limit
):
async
def
process_all_channels
(
client
,
limit
):
print
(
"Fetching all the channels the bot has joined to "
)
channels
=
await
list_joined_channels
(
client
)
channels
=
await
list_joined_channels
(
client
)
for
ch
in
channels
:
for
ch
in
channels
:
try
:
try
:
print
(
"Processing Historical Messages"
)
history
=
[
m
async
for
m
in
client
.
iter_messages
(
ch
,
limit
=
limit
)]
history
=
[
m
async
for
m
in
client
.
iter_messages
(
ch
,
limit
=
limit
)]
for
m
in
reversed
(
history
):
for
m
in
reversed
(
history
):
await
process_message_for_storage
(
m
)
await
process_message_for_storage
(
m
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
f
"History fetch failed for
{
ch
.
title
}
:
{
e
}
"
)
print
(
f
"History fetch failed for
{
ch
.
title
}
:
{
e
}
"
)
print
(
"Messages fetched and stored in file!!"
)
print
(
"Listening for real time messages"
)
@
client
.
on
(
events
.
NewMessage
())
@
client
.
on
(
events
.
NewMessage
())
async
def
on_new
(
event
):
async
def
on_new
(
event
):
...
@@ -237,14 +249,18 @@ async def main():
...
@@ -237,14 +249,18 @@ async def main():
client
=
TelegramClient
(
SESSION
,
API_ID
,
API_HASH
)
client
=
TelegramClient
(
SESSION
,
API_ID
,
API_HASH
)
await
client
.
start
()
await
client
.
start
()
print
(
"Initialized Telegram Client"
)
print
(
"------------------------------------------"
)
print
(
"Trying to join the specified channel if mentioned any!!"
)
target
=
await
ensure_joined
(
client
,
INVITE_LINK
)
target
=
await
ensure_joined
(
client
,
INVITE_LINK
)
if
target
:
if
target
:
print
(
f
"Listening to channel:
{
target
.
title
}
"
)
print
(
f
"Listening to channel:
{
target
.
title
}
"
)
await
process_single_channel
(
client
,
target
,
limit
=
50
)
await
process_single_channel
(
client
,
target
,
LIMIT
)
else
:
else
:
print
(
"Listening to all joined channels"
)
print
(
"Listening to all joined channels"
)
await
process_all_channels
(
client
,
limit
=
50
)
await
process_all_channels
(
client
,
LIMIT
)
print
(
"Listening for messages..."
)
print
(
"Listening for messages..."
)
await
client
.
run_until_disconnected
()
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