Skip to content

Commit 1352917

Browse files
committed
Add Matrix support
1 parent db8f732 commit 1352917

File tree

8 files changed

+190
-31
lines changed

8 files changed

+190
-31
lines changed

.env.example

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
DISCORD_TOKEN=
22

3-
DISCORD_FUN_REDDIT=
4-
DISCORD_SERIOUS_REDDIT=
5-
DISCORD_LIFE_REDDIT=
6-
DISCORD_COMPANY_BLOGS=
7-
DISCORD_PRODUCT_BLOGS=
8-
DISCORD_INFOSEC_BLOGS=
9-
DISCORD_INDIVIDUAL_BLOGS=
10-
DISCORD_NEWS=
11-
DISCORD_FORUMS=
12-
DISCORD_DEBUG_CHANNEL=
3+
DISCORD_FUN_REDDIT=0
4+
DISCORD_SERIOUS_REDDIT=0
5+
DISCORD_LIFE_REDDIT=0
6+
DISCORD_COMPANY_BLOGS=0
7+
DISCORD_PRODUCT_BLOGS=0
8+
DISCORD_INFOSEC_BLOGS=0
9+
DISCORD_INDIVIDUAL_BLOGS=0
10+
DISCORD_NEWS=0
11+
DISCORD_FORUMS=0
12+
DISCORD_DEBUG_CHANNEL=0
1313

1414
TELEGRAM_TOKEN=
1515
TELEGRAM_CHAT_ID=
1616

17-
TELEGRAM_FUN_REDDIT=
18-
TELEGRAM_SERIOUS_REDDIT=
19-
TELEGRAM_LIFE_REDDIT=
20-
TELEGRAM_COMPANY_BLOGS=
21-
TELEGRAM_PRODUCT_BLOGS=
22-
TELEGRAM_INDIVIDUAL_BLOGS=
23-
TELEGRAM_INFOSEC_BLOGS=
24-
TELEGRAM_FORUMS=
25-
TELEGRAM_NEWS=
17+
TELEGRAM_FUN_REDDIT=0
18+
TELEGRAM_SERIOUS_REDDIT=0
19+
TELEGRAM_LIFE_REDDIT=0
20+
TELEGRAM_COMPANY_BLOGS=0
21+
TELEGRAM_PRODUCT_BLOGS=0
22+
TELEGRAM_INDIVIDUAL_BLOGS=0
23+
TELEGRAM_INFOSEC_BLOGS=0
24+
TELEGRAM_FORUMS=0
25+
TELEGRAM_NEWS=0
26+
27+
#matrix
28+
MATRIX_HOMESERVER=
29+
MATRIX_USER=
30+
MATRIX_TOKEN=
31+
32+
MATRIX_FUN_REDDIT=
33+
MATRIX_SERIOUS_REDDIT=
34+
MATRIX_LIFE_REDDIT=
35+
MATRIX_COMPANY_BLOGS=
36+
MATRIX_PRODUCT_BLOGS=
37+
MATRIX_INDIVIDUAL_BLOGS=
38+
MATRIX_INFOSEC_BLOGS=
39+
MATRIX_FORUMS=
40+
MATRIX_NEWS=
2641

2742
#reddit auth
2843
CLIENT_ID=

data/blogs/company_blogs.opml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<outline type="rss" text="Intel" title="Intel" xmlUrl="https://community.intel.com/cipcp26785/rss/Category?category.id=tech-innovation&amp;interaction.style=blog" htmlUrl="https://community.intel.com/t5/Tech-Innovation/ct-p/tech-innovation"/>
118118
<outline type="rss" text="Intercom" title="Intercom" xmlUrl="https://www.intercom.com/blog/engineering/feed" htmlUrl="https://engineering.intercom.io/"/>
119119
<outline type="rss" text="Jane Street" title="Jane Street" xmlUrl="https://blogs.janestreet.com/feed.xml" htmlUrl="https://blogs.janestreet.com/category/ocaml/"/>
120+
<outline type="rss" text="JioCinema" title="JioCinema" xmlUrl="https://blog.jiocinema.com/rss/" htmlUrl="https://blog.jiocinema.com/"/>
120121
<outline type="rss" text="Jobandtalent" title="Jobandtalent" xmlUrl="https://jobandtalent.engineering/feed" htmlUrl="https://jobandtalent.engineering/"/>
121122
<outline type="rss" text="Just Eat" title="Just Eat" xmlUrl="https://tech.just-eat.com/feed/" htmlUrl="https://tech.just-eat.com/"/>
122123
<outline type="rss" text="King" title="King" xmlUrl="https://techblog.king.com/feed/" htmlUrl="https://techblog.king.com/"/>

data/blogs/individual_blogs.opml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<outline title="mfkl" htmlUrl="https://mfkl.github.io" xmlUrl="https://mfkl.github.io/feed.xml" />
179179
<outline title="mgsloan" htmlUrl="https://mgsloan.com" xmlUrl="https://mgsloan.com/feed.xml" />
180180
<outline title="Michael Chadwick | home of audio dev michael chadwick" htmlUrl="https://michaelchadwick.info" xmlUrl="https://michaelchadwick.info/feed.xml" />
181+
<outline title="mcyoung" htmlUrl="https://mcyoung.xyz/" xmlUrl="https://mcyoung.xyz/feed.xml" />
181182
<outline title="Miika von Bell: Full-stack software developer" htmlUrl="https://miikavonbell.com" xmlUrl="https://miikavonbell.com/index.xml" />
182183
<outline title="www.mikenikles.com" htmlUrl="https://mikenikles.com" xmlUrl="https://www.mikenikles.com/rss.xml" />
183184
<outline title="Mazzarolo Matteo · mmazzarolo.com" htmlUrl="https://mmazzarolo.com" xmlUrl="https://mmazzarolo.com/blog/index.xml" />
@@ -333,4 +334,4 @@
333334
<outline type="rss" title="Jezen Thomas | Haskell, Unix, Minimalism, and Entrepreneurship." xmlUrl="https://jezenthomas.com/feed.xml" htmlUrl="https://jezenthomas.com" />
334335
</outline>
335336
</body>
336-
</opml>
337+
</opml>

data/blogs/news.opml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<outline text="Coin Desk" title="Coin Desk" type="rss" xmlUrl="https://www.coindesk.com/arc/outboundfeeds/rss/" htmlUrl="https://www.coindesk.com/"/>
1111
<outline text="Computer Weekly" title="Computer Weekly" type="rss" xmlUrl="https://www.computerweekly.com/rss/RSS-Feed.xml" htmlUrl="https://www.computerweekly.com/"/>
1212
<outline text="Computer World" title="Computer World" type="rss" xmlUrl="https://www.computerworld.com/in/index.rss" htmlUrl="https://www.computerworld.com/"/>
13-
<outline text="Extreme Tech" title="Extreme Tech" type="rss" xmlUrl="https://www.extremetech.com/feed" htmlUrl="https://www.extremetech.com/"/>
1413
<outline text="Fast Company" title="Fast Company" type="rss" xmlUrl="https://www.fastcompany.com/technology/rss" htmlUrl="https://www.fastcompany.com/technology"/>
1514
<outline text="Information Week" title="Information Week" type="rss" xmlUrl="https://www.informationweek.com/rss.xml" htmlUrl="https://www.informationweek.com/"/>
1615
<outline text="MIT Technology Review" title="MIT Technology Review" type="rss" xmlUrl="https://www.technologyreview.com/feed/" htmlUrl="https://www.technologyreview.com/"/>

main.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from time import sleep
1111

1212
import discord
13+
import simplematrixbotlib as botlib
1314
import telegram
1415
from discord.ext import commands
1516
from dotenv import load_dotenv
@@ -19,10 +20,12 @@
1920
import forums
2021
import reddit
2122
from util.embed_utils import (RedditPost, TelegramPost, create_blog_embed,
22-
create_blog_reddit_post,
23+
create_blog_matrix_post, create_blog_reddit_post,
2324
create_blog_telegram_post, create_forum_embed,
25+
create_forum_matrix_post,
2426
create_forum_reddit_post,
2527
create_forum_telegram_post, create_reddit_embed,
28+
create_reddit_matrix_post,
2629
create_reddit_telegram_post)
2730
from util.load_config import load_config
2831

@@ -78,6 +81,23 @@
7881
'forum': os.getenv('REDDIT_FORUMS'),
7982
}
8083

84+
matrix_space = os.getenv('MATRIX_SPACE')
85+
matrix_rooms = {
86+
'reddit': {
87+
'fun': os.getenv('MATRIX_FUN_REDDIT'),
88+
'serious': os.getenv('MATRIX_SERIOUS_REDDIT'),
89+
'life': os.getenv('MATRIX_LIFE_REDDIT'),
90+
},
91+
'blog': {
92+
'company': os.getenv('MATRIX_COMPANY_BLOGS'),
93+
'product': os.getenv('MATRIX_PRODUCT_BLOGS'),
94+
'infosec': os.getenv('MATRIX_INFOSEC_BLOGS'),
95+
'news': os.getenv('MATRIX_NEWS'),
96+
'individual': os.getenv('MATRIX_INDIVIDUAL_BLOGS'),
97+
},
98+
'forum': os.getenv('MATRIX_FORUMS'),
99+
}
100+
81101

82102
class Updater: # pylint: disable=too-few-public-methods
83103
'''
@@ -137,7 +157,8 @@ class DiscordBot(commands.Bot):
137157

138158
def __init__(self, command_prefix: str, queue: JoinableQueue):
139159
self.queue = queue
140-
super().__init__(command_prefix=command_prefix)
160+
intents = discord.Intents.default()
161+
super().__init__(command_prefix=command_prefix, intents=intents)
141162

142163
async def on_ready(self):
143164
'''
@@ -189,6 +210,47 @@ def run_discord_bot(queue: JoinableQueue):
189210
discord_bot.run(DISCORD_TOKEN)
190211

191212

213+
def run_matrix_bot(queue: JoinableQueue):
214+
'''
215+
Run matrix bot
216+
'''
217+
creds = botlib.Creds(
218+
os.getenv('MATRIX_HOMESERVER'),
219+
os.getenv('MATRIX_USER'),
220+
access_token=os.getenv('MATRIX_TOKEN'))
221+
bot = botlib.Bot(creds)
222+
223+
@bot.listener.on_startup
224+
async def on_startup(room_id: str):
225+
if room_id != matrix_space:
226+
return
227+
while True:
228+
message = queue.get()
229+
if message is None:
230+
queue.task_done()
231+
break
232+
source, channel, post = message
233+
if source == 'reddit':
234+
matrix_post = create_reddit_matrix_post(channel, post)
235+
await bot.api.send_markdown_message(
236+
room_id=matrix_rooms['reddit'][channel['category']],
237+
message=matrix_post.markdown)
238+
elif source == 'blog':
239+
matrix_post = create_blog_matrix_post(post)
240+
await bot.api.send_markdown_message(
241+
room_id=matrix_rooms['blog'][channel],
242+
message=matrix_post.markdown)
243+
elif source == 'forum':
244+
matrix_post = create_forum_matrix_post(channel, post)
245+
await bot.api.send_markdown_message(
246+
room_id=matrix_rooms['forum'],
247+
message=matrix_post.markdown)
248+
queue.task_done()
249+
await asyncio.sleep(1)
250+
await bot.async_client.logout()
251+
bot.run()
252+
253+
192254
async def send_to_telegram_channel(telegram_bot: telegram.Bot, thread_id: str, post: TelegramPost):
193255
'''
194256
Send message to telegram channel
@@ -325,6 +387,12 @@ def message_sender(message_queue: JoinableQueue):
325387
queues.append(reddit_queue)
326388
reddit_process = Process(target=run_reddit_bot, args=(reddit_queue,))
327389
reddit_process.start()
390+
if os.getenv('MATRIX_USER') and os.getenv('MATRIX_TOKEN'):
391+
logging.info('Starting matrix bot')
392+
matrix_queue = JoinableQueue()
393+
queues.append(matrix_queue)
394+
matrix_process = Process(target=run_matrix_bot, args=(matrix_queue,))
395+
matrix_process.start()
328396
while True:
329397
message = message_queue.get()
330398
if message is None:

requirements-dev.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
markdownify==0.6.1
2-
markdown==3.3.4
3-
discord.py==1.6.0
4-
praw==7.1.0
2+
markdown==3.3.7
3+
discord.py==2.0.0
4+
praw==7.7.1
55
opml==0.5
66
feedparser==6.0.2
77
python-dotenv==0.19.1
88
python-telegram-bot==20.5
9+
simplematrixbotlib==2.12.0
910

1011
# Dev dependencies
1112
autopep8==1.5.7
1213
isort==5.7.0
1314
pre-commit==3.4.0
1415
pycodestyle==2.7.0
15-
pylint==2.17.6
16+
pylint==3.0.0
1617
flake8==3.9.2

requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
markdownify==0.6.1
2-
markdown==3.3.4
3-
discord.py==1.6.0
4-
praw==7.1.0
2+
markdown==3.3.7
3+
discord.py==2.0.0
4+
praw==7.7.1
55
opml==0.5
66
feedparser==6.0.2
77
python-dotenv==0.19.1
88
python-telegram-bot==20.5
9+
simplematrixbotlib==2.12.0

util/embed_utils.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def set_footer(self, **kwargs):
5656
if icon_url == '':
5757
icon_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bd/' \
5858
'Ic_account_circle_48px.svg/200px-Ic_account_circle_48px.svg.png'
59-
icon_url = f'https://images.weserv.nl/?url={icon_url}&output=png&bg=white&w=100&h=100&fit=contain&cbg=white'
59+
icon_url = f'https: //images.weserv.nl/?url={icon_url}&output=png&bg=white&w=100&h=100&fit=contain&cbg=white'
6060
self.embed.set_footer(
6161
text=text,
6262
icon_url=icon_url
@@ -319,3 +319,76 @@ def create_blog_reddit_post(post):
319319
description=description,
320320
url=url
321321
)
322+
323+
324+
@dataclass
325+
class MatrixPost:
326+
'''
327+
Matrix post
328+
'''
329+
markdown: str
330+
331+
332+
def create_forum_matrix_post(forum, post):
333+
'''
334+
Create forum embed from JSON
335+
'''
336+
config = load_config('forums.json')[forum]
337+
338+
message = f'💬 {config["publisher"]}'
339+
message += f'\n\n**[{post["title"]}]({post["url"]})**'
340+
if post["description"]:
341+
if len(post["description"]) > 1000:
342+
post["description"] = post["description"][:1000] + '...'
343+
message += f'\n\n{post["description"]}'
344+
if 'comments_link' in post:
345+
while ' ' in post['comments_link']:
346+
post['comments_link'] = post['comments_link'].replace(' ', ' ')
347+
message += f'\n\n{post["comments_link"]}'
348+
message += '\n‎'
349+
350+
return MatrixPost(
351+
markdown=message
352+
)
353+
354+
355+
def create_blog_matrix_post(post):
356+
'''
357+
Create blog post embed from JSON
358+
'''
359+
message = f'📰 {post["publisher"]}'
360+
message += f'\n\n**[{post["title"]}]({post["url"]})**'
361+
image_url = post["image"] if 'image' in post and len(post['image']) > 0 else ''
362+
if image_url:
363+
message += f'\n\n![{post["title"]}]({image_url})'
364+
if post["description"]:
365+
if len(post["description"]) > 1000:
366+
post["description"] = post["description"][:1000] + '...'
367+
message += f'\n\n{post["description"].strip()}'
368+
message += '\n‎'
369+
while '\n\n\n' in message:
370+
message = message.replace('\n\n\n', '\n\n')
371+
372+
return MatrixPost(
373+
markdown=message
374+
)
375+
376+
377+
def create_reddit_matrix_post(subreddit, post):
378+
'''
379+
Create reddit post embed from subreddit JSON
380+
'''
381+
message = f'💬 r/{subreddit["subreddit"]}'
382+
message += f'\n\n**[{post["title"]}]({post["url"]})**'
383+
image_url = post["media"]["url"] if 'media' in post and 'url' in post['media'] else ''
384+
if image_url:
385+
message += f'\n\n![{post["title"]}]({image_url})'
386+
if post["selftext"]:
387+
if len(post["selftext"]) > 1000:
388+
post["selftext"] = post["selftext"][:1000] + '...'
389+
message += f'\n\n{post["selftext"].strip()}'
390+
message += '\n‎'
391+
392+
return MatrixPost(
393+
markdown=message
394+
)

0 commit comments

Comments
 (0)