Skip to content

Commit

Permalink
feat: addendum readme.md and .env
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekski1 committed Jun 30, 2024
1 parent f3a693b commit d253b26
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
URL=apiurl
PYROGRAM_SESSION_STRING=sessionname
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ public.pem
*.session
parser.json
test_site.py

### Docker ###
Dockerfile
docker-compose.yml
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Telegram Message Parser Bot

## Overview
This Python script uses a Pyrogram to analyze messages from Telegram channels and send them to a Fast API or another server. If a failure occurs during message delivery (status code is not 200), it saves the message in a JSON file and tries again after successful delivery of a new received message from the telegram channel.

## Installation
### Requirements
- Python 3.11+
- Poetry

### Installation Steps
1. Clone the repository:
```bash
git clone https://github.com/one-zero-eight/telegram-userbot-parser.git
cd telegram-userbot-parser
2. Install dependencies using Poetry:
```terminal
poetry install --no-root --with code-style
3. Getting the API for the user bot:
- go to the website https://core.telegram.org/api/obtaining_api_id and use the account to get the API. Be sure to write down the api_id and api_hash that you will receive on the site.
- Create a Python file and use this code to get your session file to use user bot. write down your data in api_id and api_hash. Run this file and go through all the necessary steps. You can read more here https://docs.pyrogram.org/intro/quickstart
```python
import asyncio
from pyrogram import Client

api_id = 12345
api_hash = "0123456789abcdef0123456789abcdef"


async def main():
async with Client("my_account", api_id, api_hash) as app:
await app.send_message("me", "Greetings from **Pyrogram**!")


asyncio.run(main())

4. Using the example .env.example, fill it in with your data. In the "url", specify the address of your server. In "PROGRAM_SESSION_STRING" - specify the name of your session file for program

5. Run the file pyrogram_parser.py
```python
python pyrogram_parser.py
### Futures
- Parses messages from specified Telegram channels.
- Sends parsed data to a server via HTTP POST requests.
- Retries sending messages stored in a JSON file upon server error.
### Technologies Used
- Python 3.11+
- Pyrogram
- FastAPI
- httpx
- dotenv
- Docker, Docker Compose
### License
This project is licensed under the MIT License - see the LICENSE file for details.
141 changes: 139 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Pyrogram = "^2.0.106"
pydantic = "^2.7.4"
httpx = "^0.27.0"
asyncio = "^3.4.3"
python-dotenv = "^1.0.1"

[tool.poetry.group.dev]
optional = true
Expand Down
21 changes: 13 additions & 8 deletions pyrogram_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import httpx
import os
import asyncio
from dotenv import load_dotenv

# Loading parameters from .env
load_dotenv()

# File for saving data
filename = "parser.json"
# specify the name of the session file for userbot
app = Client("my_account")
app = Client(f"{os.getenv("PYROGRAM_SESSION_STRING")}")
# The url of the server
url = "http://127.0.0.1:5000/api/messages"
url = os.getenv("URL")

print(url)


class Chat(BaseModel):
Expand All @@ -26,8 +32,8 @@ class StructureMessage(BaseModel):
sender_chat: Chat
date: datetime
chat: Chat
text: str = None
caption: str = None
text: str | None
caption: str | None

def serializableDict(self):
"""Converts an object into a dictionary with date conversion to an ISO format string"""
Expand Down Expand Up @@ -71,7 +77,7 @@ async def resend_all_message():
print(
f"Status code: {response.status_code}, message id: {history_message['id']}"
)
if response.status_code != 200:
if response.status_code not in [200, 201]:
return
file.seek(0)
json.dump([], file, ensure_ascii=False, indent=4)
Expand All @@ -84,7 +90,7 @@ async def send_to_server(message_save):
try:
response = await client.post(url, json=message_save.serializableDict())
print(f"Status code: {response.status_code}, message id: {message_save.id}")
if response.status_code == 200:
if response.status_code in [200, 201]:
await resend_all_message()
else:
await saveJson(message_save)
Expand All @@ -99,8 +105,7 @@ async def send_to_server(message_save):
async def new_message_handler(client, message):
"""Processes new messages and saves them if there is a text or signature"""
if message.text or message.caption:
message = str(message)
message_save = StructureMessage.model_validate_json(message)
message_save = StructureMessage.model_validate(message, from_attributes=True)
await send_to_server(message_save)
# saveJson(message)

Expand Down

0 comments on commit d253b26

Please sign in to comment.