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
3ecc40fd
Commit
3ecc40fd
authored
Oct 29, 2025
by
Pranav
Browse files
Implemented function to write the telegram data in json file
parent
3f8d30ae
Changes
2
Hide whitespace changes
Inline
Side-by-side
Desktop/Telegram_Bot/docker-compose.yaml
View file @
3ecc40fd
...
...
@@ -6,6 +6,8 @@ services:
container_name
:
telegram_bot
env_file
:
-
.env
environment
:
-
DATA_DIR=/app/data
volumes
:
-
./session_name.session:/app/session_name.session
-
./data:/app/data
...
...
Desktop/Telegram_Bot/services/telegram_listener.py
View file @
3ecc40fd
...
...
@@ -2,9 +2,11 @@
import
asyncio
import
csv
import
os
import
json
from
pathlib
import
Path
from
dotenv
import
load_dotenv
from
dotenv
import
load_dotenv
,
find_dotenv
from
datetime
import
datetime
from
telethon
import
TelegramClient
,
events
from
telethon.errors
import
UserAlreadyParticipantError
...
...
@@ -14,7 +16,7 @@ from telethon.tl.types import InputChannel, Channel, ReactionEmoji, ReactionCust
from
transformers
import
pipeline
load_dotenv
(
dotenv_path
=
r
"C:\Users\prana\Desktop\Telegram_Bot\.env"
)
load_dotenv
(
find_dotenv
())
# Please create a .env file. Add the environment variables over there. And never the env file on github or gitlab.
API_ID
=
int
(
os
.
getenv
(
"API_ID"
))
...
...
@@ -22,6 +24,13 @@ API_HASH = os.getenv("API_HASH")
SESSION
=
os
.
getenv
(
"SESSION"
)
INVITE_LINK
=
os
.
getenv
(
"INVITE_LINK"
)
DATA_DIR
=
Path
(
os
.
getenv
(
"DATA_DIR"
,
"/app/data"
))
DATA_DIR
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
timestamp
=
datetime
.
now
().
strftime
(
"%Y-%m-%d_%H-%M-%S"
)
JSON_PATH
=
DATA_DIR
/
f
"messages_
{
timestamp
}
.json"
sentiment_analyzer
=
pipeline
(
"sentiment-analysis"
)
# These are the headers for the csv file.
...
...
@@ -87,6 +96,24 @@ def write_csv_row(row_dict):
writer
.
writerow
(
row_dict
)
def
write_json_entry
(
entry
):
"""Append a single JSON entry to a file."""
try
:
existing_data
=
[]
if
JSON_PATH
.
exists
():
with
open
(
JSON_PATH
,
"r"
,
encoding
=
"utf-8-sig"
)
as
f
:
try
:
existing_data
=
json
.
load
(
f
)
except
json
.
JSONDecodeError
:
existing_data
=
[]
existing_data
.
append
(
entry
)
with
open
(
JSON_PATH
,
"w"
,
encoding
=
"utf-8"
)
as
f
:
json
.
dump
(
existing_data
,
f
,
ensure_ascii
=
False
,
indent
=
4
)
except
Exception
as
e
:
print
(
f
"JSON write error:
{
e
}
"
)
def
safe_serialize_reactions
(
message
):
r
=
getattr
(
message
,
"reactions"
,
None
)
if
not
r
or
not
getattr
(
r
,
"results"
,
None
):
...
...
@@ -115,7 +142,8 @@ async def process_message_for_csv(message):
"sentiment"
:
sentiment
[
0
][
"label"
]
if
sentiment
else
""
}
try
:
write_csv_row
(
row
)
#write_csv_row(row)
write_json_entry
(
row
)
except
Exception
as
e
:
print
(
f
"CSV write error:
{
e
}
"
)
...
...
@@ -199,7 +227,6 @@ async def main():
if
target
:
print
(
"Listening to single channel "
)
process_last_message_for_one_channel
(
client
,
process_message
,
target
,
limit
=
5
)
# After collecting
else
:
print
(
"No INVITE_URL provided. Listening to all chats."
)
...
...
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