Skip to content

Commit

Permalink
Improved variables to format message and button
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielRF committed Jul 13, 2022
1 parent 00a9418 commit b8e2ad0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ Defina as variáveis na aba `Secrets` do repositório:

`URL`: Endereços de feeds RSS separados por vírgulas;

`EMOJIS`: Emojis separados por vírgulas usados na composição do botão;
`MESSAGE_TEMPLATE`: (opcional) Texto da mensagem. Valor padrão: `<b>{TITLE}</b>` ([ver opções](#opções-de-variáveis));

`MESSAGE_TEMPLATE`: (opcional) Texto da mensagem. Valor padrão: `<b>{topic["title"]}</b>` ([ver opções](#opções-de-variáveis));
`BUTTON_TEXT`: (opcional) Texto do botão com o link. Sugestão: `{SITE_NAME]}`. Se esta variável não for criada não será enviado um botão. ([Ver opções](#opções-de-variáveis));

`BUTTON_TEXT`: (opcional) Texto do botão com o link. Sugestão: `{topic['site_name']}`. Se esta variável não for criada não será enviado um botão.
`EMOJIS`: (opcional) Emojis separados por vírgulas. Podem ser usados na mensagem ou no botão;

### Opções de variáveis

`{topic['site_name']}`: Nome do site;
`{SITE_NAME}`: Nome do site;

`{topic['title']}`: Título do post;
`{TITLE}`: Título do post;

`{topic['summary']}`: Sumário do post;
`{SUMMARY}`: Sumário do post;

`{topic['link']}`: Link do post.
`{LINK}`: Link do post;

`{EMOJI}`: Emoji escolhido aleatoriamente da lista.

## Uso

Expand Down
32 changes: 28 additions & 4 deletions rss2telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from time import gmtime
import feedparser
import os
import re
import telebot
import time
import random
Expand Down Expand Up @@ -34,12 +35,16 @@ def check_history(link):
return data

def send_message(topic, button):
MESSAGE_TEMPLATE = os.environ.get(f'MESSAGE_TEMPLATE', f'<b>{topic["title"]}</b>')
MESSAGE_TEMPLATE = os.environ.get(f'MESSAGE_TEMPLATE', False)
if MESSAGE_TEMPLATE:
MESSAGE_TEMPLATE = set_env_vars(MESSAGE_TEMPLATE, topic)
else:
MESSAGE_TEMPLATE = f'<b>{topic["title"]}</b>'

btn_link = button
if button:
btn_link = types.InlineKeyboardMarkup()
btn = types.InlineKeyboardButton(f'{random.choice(EMOJIS.split(","))} {button}', url=topic['link'])
btn = types.InlineKeyboardButton(f'{button}', url=topic['link'])
btn_link.row(btn)

if topic['photo']:
Expand All @@ -50,7 +55,8 @@ def send_message(topic, button):
try:
bot.send_photo(dest, photo, caption=MESSAGE_TEMPLATE, parse_mode='HTML', reply_markup=btn_link)
except telebot.apihelper.ApiTelegramException:
send_message(topic, False)
topic['photo'] = False
send_message(topic, button)
else:
for dest in DESTINATION.split(','):
bot.send_message(dest, MESSAGE_TEMPLATE, parse_mode='HTML', reply_markup=button, disable_web_page_preview=True)
Expand All @@ -66,6 +72,22 @@ def get_img(url):
photo = False
return photo

def set_env_vars(text, topic):
cases = {
'SITE_NAME': topic['site_name'],
'TITLE': topic['title'],
'SUMMARY': topic['summary'],
'LINK': topic['link'],
'EMOJI': random.choice(EMOJIS.split(","))
}
for word in re.split('{|}', text):
try:
text = text.replace(word, cases.get(word))
except TypeError:
continue
return text.replace('\\n', '\n').replace('{', '').replace('}', '')


def check_topics(url):
now = gmtime()
feed = feedparser.parse(url)
Expand All @@ -82,7 +104,9 @@ def check_topics(url):
topic['summary'] = tpc.summary
topic['link'] = tpc.links[0].href
topic['photo'] = get_img(tpc.links[0].href)
BUTTON_TEXT = os.environ.get(f'BUTTON_TEXT', False)
BUTTON_TEXT = os.environ.get('BUTTON_TEXT', False)
if BUTTON_TEXT:
BUTTON_TEXT = set_env_vars(BUTTON_TEXT, topic)
send_message(topic, BUTTON_TEXT)
add_to_history(topic['link'])

Expand Down

0 comments on commit b8e2ad0

Please sign in to comment.