Skip to content

Commit

Permalink
Add the option to send a message to DEV_CHAT_ID chat when the URL is …
Browse files Browse the repository at this point in the history
…not filtered.

If the DEV_CHAT_ID environment variable is set, the bot will send a
message to that chat if it fails to filter the URL or the product
code is not found, unless the url is base_url.
  • Loading branch information
Aritzherrero4 committed Jul 23, 2023
1 parent 33435e2 commit d338926
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ If you find different URS schemes, they can be added to the regular expression a

## Env variables

There are 4 required env variables that must be configured in Heroku to work.
There are 3 required env variables that must be configured in Heroku to work.

* ```affiliate_tag``` : The affiliate tag you want to use for the generated URL.

* ```search_url```: The base URL used for the link. For example "amazon.es" for the Spanish Amazon website products. The URL must be provided without `http`/`https` and without `www.`

* ```TOKEN```: The API Token of the telegram bot. This can be obtained after creating a bot with botFather.

There is an optional variable that can be set with the chat_id of the developer with the bot to receive messages when an URL is not filtered or the product code is not found.

* ```DEV_CHAT_ID```: Telegram chat id of the chat to send the messages

## How to create your own bot

If you want to create your own bot, fork or clone this repo and configure it to run locally.
Expand Down
9 changes: 8 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Read env variables
TOKEN = os.environ['TOKEN']
DEV_CHAT_ID = os.environ['DEV_CHAT_ID'] if 'DEV_CHAT_ID' in os.environ else None
search_url = os.environ['search_url']
affiliate_tag = os.environ['affiliate_tag']

Expand All @@ -40,7 +41,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
update: The incoming update.
context: The context of the bot.
"""
await context.bot.send_message(chat_id=update.effective_chat.id, text="Hola! Este bot responde a los enlaces de amazon añadiendo un codigo de afiliado!")
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"Hola! Este bot responde a los enlaces de amazon añadiendo un codigo de afiliado!")

def create_affiliate_url(product_code: str) -> str:
"""Create a new URL with the the product code and the affiliate tag.
Expand Down Expand Up @@ -105,9 +106,15 @@ async def filterText(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None
# Create and send the new url with the affiliate tag
new_url = create_affiliate_url(pCode)
logger.info(f"Filtered link: {msg} -> {new_url}" if m != None else f"Product code not found: {msg} -> {new_url}")

if DEV_CHAT_ID is not None and msg != base_url:
await context.bot.sendMessage(chat_id=DEV_CHAT_ID, text=f'Product code not found! Original URL: {msg} ')

await context.bot.sendMessage(chat_id=update.message.chat_id, reply_to_message_id=update.effective_message.id, text=new_url)
else:
logger.warning(f'URL not filtered: {msg}')
if DEV_CHAT_ID is not None:
await context.bot.sendMessage(chat_id=DEV_CHAT_ID, text=f'URL not filtered: {msg}')

def main():
"""Start the bot."""
Expand Down

0 comments on commit d338926

Please sign in to comment.