Created
June 12, 2021 15:01
-
-
Save Loskir/206386d40c8c4929cc4371ef85e8a8e3 to your computer and use it in GitHub Desktop.
Telegraf v3 middleware for handling block/unblock events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Events = require('../models/events') | |
const Users = require('../models/users') | |
module.exports = async (ctx, next) => { | |
if (ctx.update?.my_chat_member?.chat?.type !== 'private') { | |
return next() | |
} | |
if (!ctx.update?.my_chat_member?.new_chat_member?.status) { | |
return next() | |
} | |
const user_id = ctx.update.my_chat_member.chat.id | |
if (ctx.update?.my_chat_member?.new_chat_member?.status === 'kicked') { | |
ctx.log.info('blocked') | |
await Events.create({ | |
user_id, | |
type: 'block', | |
}) | |
ctx.user = await Users.findOneAndUpdate({user_id}, { | |
$set: { | |
is_blocked: true, | |
}, | |
}) | |
} else if (ctx.update?.my_chat_member?.new_chat_member?.status === 'member') { | |
ctx.log.info('unblocked') | |
await Events.create({ | |
user_id, | |
type: 'unblock', | |
}) | |
ctx.user = await Users.findOneAndUpdate({user_id}, { | |
$set: { | |
is_blocked: false, | |
is_deactivated: false, | |
}, | |
}) | |
} else { | |
ctx.log.info(`unknown new chat member status: ${ctx.update?.my_chat_member?.new_chat_member?.status}`) | |
} | |
return next() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment