forked from ehcaning/divar-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add src directory and create project tree
make separate files for schemas and telegram
- Loading branch information
Showing
5 changed files
with
78 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
# AD class model | ||
class AD(BaseModel): | ||
title: str | ||
price: int | ||
description: str = "" | ||
district: str | ||
images: list[str] = [] | ||
token: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
this file is for storing telegram bot functions and modules to send message from telegram | ||
""" | ||
|
||
|
||
import logging | ||
import telegram | ||
from src.schemas import AD | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(name=__name__) | ||
|
||
|
||
async def send_telegram_message(bot: telegram.Bot, user_chat_id: int, ad: AD): | ||
text = f"🗄 <b>{ad.title}</b>" + "\n" | ||
text += f"📌 محل آگهی : <i>{ad.district}</i>" + "\n" | ||
_price = f"{ad.price:,} تومان" if ad.price else "توافقی" | ||
text += f"💰 قیمت : {_price}" + "\n\n" | ||
text += f"📄 توضیحات :\n{ad.description}" + "\n" | ||
text += f"https://divar.ir/v/a/{ad.token}" | ||
|
||
# send single photo | ||
if len(ad.images) == 1: | ||
await bot.send_photo( | ||
caption=text, photo=ad.images[0], chat_id=user_chat_id, parse_mode="HTML" | ||
) | ||
# send album | ||
elif len(ad.images) > 1: | ||
_media_list = [telegram.InputMediaPhoto(img) for img in ad.images[:10]] | ||
try: | ||
await bot.send_media_group( | ||
caption=text, media=_media_list, chat_id=user_chat_id, parse_mode="HTML" | ||
) | ||
except telegram.error.BadRequest as e: | ||
logger.error(f"Error sending photos : {e}") | ||
return | ||
else: | ||
# send just text | ||
await bot.send_message(text=text, chat_id=user_chat_id, parse_mode="HTML") |