BotKit by Fedify :botkit:'s avatar
BotKit by Fedify :botkit:

@[email protected]

now allows your bot to like or unlike other messages!

Automatically liking mentions

It is simple to automatically like mentions of your bot. You can use the onMention event handler and the Message.like() method together:

bot.onMention = async (session, message) => {
  await message.like();
};Liking a message

You can like a message by calling the like() method:

const message = await session.publish(
  text`This message will be liked.`
);
await message.like();

CAUTION

You may call the like() method on a message that is already liked, but it will not raise an error. Under the hood, such a call actually sends multiple Like activities to the fediverse, whose behavior is undefined—some servers may ignore the duplicated activities, some servers may allow them and count them as multiple likes.

If you need to undo the liking, you can call the unlike() method:

const like = await message.like();
await like.unlike();
BotKit by Fedify :botkit:'s avatar
BotKit by Fedify :botkit:

@[email protected] · Reply to BotKit by Fedify :botkit:'s post

… and also listen to likes or undoing them on messages by your bot!

Like

The onLike event handler is called when someone likes messages on your bot or actors your bot follows. It receives a Like object, which represents the like activity, as the second argument.

The following is an example of a like event handler that sends a direct message when someone likes a message on your bot:

bot.onLike = async (session, like) => {
  if (like.message.actor.id?.href !== session.actorId.href) return;
  await session.publish(
    text`Thanks for liking my message, ${like.actor}!`,
    { visibility: "direct" },
  );
};

Unlike

The onUnlike event handler is called when someone undoes a Like activity on messages on your bot or actors your bot follows. It receives a Like object, which represents the Like activity which was undone, as the second argument.

The following is an example of an unlike event handler that sends a direct message when someone undoes a like activity on a message on your bot:

bot.onUnlike = async (session, like) => {
  if (like.message.actor.id?.href !== session.actorId.href) return;
  await session.publish(
    text`I'm sorry to hear that you unliked my message, ${like.actor}.`,
    { visibility: "direct" },
  );
};