Python code for a telegram bot that bring content of certain links to telegra.ph.
pip install -r requirements.txt
Put your bot token and telegraph api token in tokens.py
(add this file to .gitignore after doing so), and then
python3.9 bot.py
Python 3.9 is recommended. The dependent lxml
is buggy for Python 3.10+.
Note: If you want to use this in a group, talk to @botfather on telegram to turn off group privacy mode.
In the code we included a sample previewer for caus.com.
import re
from bs4 import BeautifulSoup
from preview import Preview
class Caus(Preview):
def __init__(self, pattern:re.Pattern) -> None:
super(Caus, self).__init__(pattern)
async def clean_url(self, URL:str) -> str:
URL = URL.replace('m.caus.com', 'www.caus.com')
if not URL.startswith("http"):
URL = "https://" + URL
return URL
def viewer(self, r):
soup = BeautifulSoup(r.text, 'html.parser')
title = soup.find("div",{"class":"text_title"}).text
author = None
content = soup.find("div",{"class":"img-wrapper"})
return (title, author, content)
PATTERN = re.compile(r'\b(https?://)?\S*\.caus\.com/detail/\S*')
caus = Caus(PATTERN)
To add your own, create a file follow this pattern. And add it to bot.py
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
- The file
utils/clean_html.py
is adapted from mercuree/html-telegraph-poster. - The file
utils/html_to_nodes.py
is adapted from python273/telegraph