aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2019-07-17 00:00:47 +0200
committerPatrick Spek <p.spek@tyil.nl>2019-07-17 00:00:47 +0200
commit03ca09761ad010633c9f0093e43c8f4794210680 (patch)
tree4341b8cb2ebcecc2ae075f80dd7eba4b0b44eeb6
parenta8b30d55f3e746c6de09de4cc4ce6ee4f5d880a7 (diff)
downloadMatrix::Bot-03ca09761ad010633c9f0093e43c8f4794210680.tar.gz
Matrix::Bot-03ca09761ad010633c9f0093e43c8f4794210680.tar.bz2
Introduce Markdown
-rw-r--r--CHANGELOG.md20
-rw-r--r--META6.json3
-rw-r--r--lib/Matrix/Bot.pm629
3 files changed, 49 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5fa7f54..bd182d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,15 @@ Versioning](http://semver.org/spec/v2.0.0.html).
been set up correctly, and the bot can start responding to events that happen
on Matrix.
+- The `Matrix::Bot` object now has an additional method to send Markdown
+ formatted text to a room, `send-markdown`. This convenience method allows one
+ to simply return a Markdown formatted string, and the framework will handle
+ the rendering.
+
+ Related links:
+
+ - https://github.com/retupmoca/p6-markdown
+
### Changed
- The `handle-room-text` method now sends `m.notice` type messages. The Matrix
@@ -50,6 +59,17 @@ Versioning](http://semver.org/spec/v2.0.0.html).
change is intended to provide more power to the event handling methods of a
plugin.
+- `handle-room-text` now uses `Text::Markdown` to support markup in text.
+
+ Related links:
+
+ - [`GitLab#1`](https://gitlab.com/tyil/perl6-matrix-bot/issues/1)
+ - https://github.com/retupmoca/p6-markdown
+
+ Special thanks to:
+
+ - [matiaslina](https://gitlab.com/matiaslina)
+
## [0.1.0] - 2019-07-15
- Initial release
diff --git a/META6.json b/META6.json
index 76bd97d..52a1a6d 100644
--- a/META6.json
+++ b/META6.json
@@ -5,7 +5,8 @@
],
"depends": [
"LogP6",
- "Matrix::Client"
+ "Matrix::Client",
+ "Text::Markdown"
],
"description": "A framework for writing Matrix bots",
"license": "AGPL-3.0",
diff --git a/lib/Matrix/Bot.pm6 b/lib/Matrix/Bot.pm6
index 8d03209..d7b9f42 100644
--- a/lib/Matrix/Bot.pm6
+++ b/lib/Matrix/Bot.pm6
@@ -3,11 +3,13 @@
use v6.d;
use LogP6;
+use Matrix::Client;
+use Text::Markdown;
+
use Matrix::Bot::EventWrapper::InitialSync;
use Matrix::Bot::EventWrapper::RoomInvite;
use Matrix::Bot::EventWrapper::RoomTextMessage;
use Matrix::Bot::Plugin;
-use Matrix::Client;
#| A small framework to build Matrix bots upon.
unit class Matrix::Bot;
@@ -227,13 +229,36 @@ method run ()
# If the handler returns a string, conveniently send it back to the
# originating channel.
if ($reply ~~ Str:D && $reply ne "") {
- $!client.send($event.room_id, $reply, :type<m.notice>);
+ self.send-markdown($event.room_id, $reply);
}
})
}
}
}
+#| Convenience method to send rendered Markdown to a room.
+method send-markdown (
+ #| The room ID to send the message to.
+ Matrix::RoomID:D $room-id,
+
+ #| The Markdown body to send.
+ Str:D $body,
+
+ #| The message type to use.
+ Str:D $message-type = "m.notice",
+) {
+ $!client.send-event(
+ $room-id,
+ event-type => "m.room.message",
+ content => {
+ msgtype => $message-type,
+ body => $body,
+ format => "org.matrix.custom.html",
+ formatted_body => parse-markdown($body).to-html,
+ },
+ );
+}
+
#| Convenience method to distribute an event to all plugins. This method will
#| also ensure the handlers will run in their own threads, and that exceptions
#| are caught and handled slightly more gracefully.