implement markdown support for matrix bot, closes #2
This commit is contained in:
parent
217378dff9
commit
315ff32422
9
main.py
9
main.py
@ -29,10 +29,17 @@ async def main():
|
|||||||
monitor_thread = threading.Thread(target=monitor.log_loop, args=(15,))
|
monitor_thread = threading.Thread(target=monitor.log_loop, args=(15,))
|
||||||
monitor_thread.start()
|
monitor_thread.start()
|
||||||
|
|
||||||
|
# Send "online message"
|
||||||
|
try:
|
||||||
|
await bot.send_markdown("**MBOT ONLINE**")
|
||||||
|
await bot.logout()
|
||||||
|
except nio.exceptions.LocalProtocolError:
|
||||||
|
pass
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
event = queue.get()
|
event = queue.get()
|
||||||
try:
|
try:
|
||||||
await bot.send_message(event)
|
await bot.send_markdown(event)
|
||||||
await bot.logout()
|
await bot.logout()
|
||||||
queue.task_done()
|
queue.task_done()
|
||||||
except nio.exceptions.LocalProtocolError:
|
except nio.exceptions.LocalProtocolError:
|
||||||
|
23
matrix.py
23
matrix.py
@ -1,3 +1,4 @@
|
|||||||
|
import markdown
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from nio import AsyncClient, LoginResponse
|
from nio import AsyncClient, LoginResponse
|
||||||
|
|
||||||
@ -28,6 +29,28 @@ class MatrixBot:
|
|||||||
)
|
)
|
||||||
logger.info("Message sent")
|
logger.info("Message sent")
|
||||||
|
|
||||||
|
async def send_markdown(self, message: str):
|
||||||
|
if not self.client.access_token:
|
||||||
|
logged_in = await self.login()
|
||||||
|
if not logged_in:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Convert message to markdown
|
||||||
|
html = markdown.markdown(message)
|
||||||
|
|
||||||
|
# Send markdown formatted message
|
||||||
|
await self.client.room_send(
|
||||||
|
room_id=self.config['room_id'],
|
||||||
|
message_type="m.room.message",
|
||||||
|
content={
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"body": message,
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": html
|
||||||
|
}
|
||||||
|
)
|
||||||
|
logger.info("Markdown message sent")
|
||||||
|
|
||||||
async def login(self):
|
async def login(self):
|
||||||
response = await self.client.login(
|
response = await self.client.login(
|
||||||
password=self.config['password']
|
password=self.config['password']
|
||||||
|
@ -3,3 +3,4 @@ loguru
|
|||||||
web3
|
web3
|
||||||
requests
|
requests
|
||||||
SQLAlchemy
|
SQLAlchemy
|
||||||
|
markdown
|
Loading…
Reference in New Issue
Block a user