Skip to content

Commit

Permalink
Introduce reactions (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzadp authored Aug 15, 2024
1 parent b1e2b99 commit 06040bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"devDependencies": {
"@eng-automation/js-style": "^3.1.0",
"@eng-automation/testing": "^1.4.0",
"@eng-automation/testing": "^1.5.1",
"@types/body-parser": "^1.19.2",
"@types/express": "^4.17.13",
"@types/jest": "^29.5.12",
Expand Down
18 changes: 15 additions & 3 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const sendMessage = (roomId: string, msg: string, formattedMsg?: string) => {
bot.sendEvent(roomId, null, "m.room.message", msgObject, "").catch((e) => logger.error(e));
};

const sendReaction = (roomId: string, eventId: string, emoji: string) => {
const msgObject: mSDK.IContent = { "m.relates_to": { event_id: eventId, key: emoji, rel_type: "m.annotation" } };

bot.sendEvent(roomId, null, "m.reaction", msgObject, "").catch((e) => logger.error(e));
};

const printHelpMessage = (roomId: string, message = "") =>
sendMessage(
roomId,
Expand All @@ -73,11 +79,12 @@ bot.on(mSDK.RoomEvent.MyMembership, (room: mSDK.Room, membership: string) => {
bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
const sender = event.getSender();
const roomId = event.getRoomId();
const eventId = event.getId();
const { body } = event.getContent<{ body: string }>();

if (roomId === undefined) {
if (roomId === undefined || eventId == undefined) {
// Should never happen for a "Room.timeline" event
throw new Error("roomId is not defined");
throw new Error("roomId or eventId is not defined");
}

// only act on messages
Expand Down Expand Up @@ -114,8 +121,10 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
logger.error("⭕ An error occurred when checking the balance", e);
});
} else if (action === "!drip") {
sendReaction(roomId, eventId, "👀");
if (!arg0) {
logger.warn("Address not provided, skipping");
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, `${sender} Please provide an address to be funded.`);
return;
}

Expand All @@ -127,6 +136,7 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {
try {
AccountId().enc(address);
} catch (e) {
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, `${sender} provided an incompatible address.`);
return;
}
Expand Down Expand Up @@ -163,9 +173,11 @@ bot.on(mSDK.RoomEvent.Timeline, (event: mSDK.MatrixEvent) => {

const message = formattedSuccessfulDripResponse(dripAmount, sender, res.hash);

sendReaction(roomId, eventId, "👍");
sendMessage(roomId, message.plain, message.formatted);
})
.catch((e) => {
sendReaction(roomId, eventId, "😕");
sendMessage(roomId, "An unexpected error occurred, please check the server logs");
logger.error("⭕ An error occurred when dripping", e);
});
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,15 @@ __metadata:
languageName: node
linkType: hard

"@eng-automation/testing@npm:^1.4.0":
version: 1.4.0
resolution: "@eng-automation/testing@npm:1.4.0"
"@eng-automation/testing@npm:^1.5.1":
version: 1.5.1
resolution: "@eng-automation/testing@npm:1.5.1"
dependencies:
"@eng-automation/js": "npm:^2.1.0"
"@octokit/rest": "npm:^19.0.7"
mockttp: "npm:^3.9.4"
selfsigned: "npm:^2.1.0"
checksum: 10c0/b71a5adf52feb6e19aa851db1c478febb9fb23220b29f16e06b05bea79a9b1601a58016065d10fe97ec37101fd74a0addf28f84f1709f6131e70a43bf2b73c22
checksum: 10c0/7582e24b39f339febc864b8fcfe8f32c943c662843aec614d236a6b3ab51afe6ec1b349a5bf45fd9d7c3de8a19c8bb01656491a367034b5dc88b4ac9d37befca
languageName: node
linkType: hard

Expand Down Expand Up @@ -4985,7 +4985,7 @@ __metadata:
dependencies:
"@eng-automation/js": "npm:^2.1.0"
"@eng-automation/js-style": "npm:^3.1.0"
"@eng-automation/testing": "npm:^1.4.0"
"@eng-automation/testing": "npm:^1.5.1"
"@polkadot-labs/hdkd": "npm:^0.0.6"
"@polkadot-labs/hdkd-helpers": "npm:^0.0.6"
"@types/body-parser": "npm:^1.19.2"
Expand Down

0 comments on commit 06040bf

Please sign in to comment.