Telegram User Subscription Checker
# 🤝help
v
I'm trying to add a function to my telegram bot that will check if user is subscribed to my telegram channel. How can I do it?

https://cdn.discordapp.com/attachments/1139081666862788658/1139099857957691452/image.png

I've tried to use this code created by botpress, but it seems it doesn't work
I have changed my code this way, but it still doesn't work const telegramWebhookUrl = "https://api.telegram.org/bot/getUpdates" const response = await axios.get(telegramWebhookUrl) const telegramId = _.get(response, 'data.result[0].message.from.id', null) const channel1 = user.channel1 const channel2 = user.channel2 const isSubscribedToChannel1 = await checkSubscription(telegramId, channel1) const isSubscribedToChannel2 = await checkSubscription(telegramId, channel2) if (isSubscribedToChannel1 && isSubscribedToChannel2) { console.log("User is subscribed to both channels") workflow.chan1 = 'yes' workflow.chan2 = 'yes' } else { console.log("User is not subscribed to both channels") workflow.chan1 = 'no' workflow.chan2 = 'no' } async function checkSubscription(telegramId, channel) { const subscriptionUrl =
https://api.telegram.org/bot<(MyAPI)>/getChatMember?chat_id=${channel}&user_id=${telegramId}
const response = await axios.get(subscriptionUrl) const status = _.get(response, 'data.result.status', null) return status === 'member' || status === 'creator' }
I've made it to this point and now I just need to get user's telegram id

https://cdn.discordapp.com/attachments/1139081666862788658/1139197180704473249/image.png

Just in case you want to use this code: const telegramId = event.tags.conversation['telegram:id'] const channel1 = user.channel1 console.log('telegramId:', telegramId); console.log('channel1:', channel1); const isSubscribedToChannel1 = await checkSubscription(telegramId, channel1) if (isSubscribedToChannel1) { console.log('User is subscribed to both channels') workflow.chan1 = 'yes' workflow.chan2 = 'yes' } else { console.log('User is not subscribed to both channels') workflow.chan1 = 'no' workflow.chan2 = 'no' } async function checkSubscription(telegramId, channel) { const url =
https://api.telegram.org/bot${bot.telegramToken}/getChatMember?chat_id=${channel}&user_id=${telegramId}
const response = await axios.get(url) const status = _.get(response, 'data.result.status') return status === 'member' || status === 'creator' }
c
@bright-magazine-792 @early-train-33247 can any of you two help here? 🙂 thanks a lot
v
Here's my latest try to achieve desired result. I changed the code and have checked the general and local botpress logs. Still hoping to get some help! const telegramId = event.tags.conversation['telegram:id'] const chatId = event.tags.conversation["telegram:chat_id"] const channel1 = user.channel1 console.log('telegramId:', telegramId); console.log('chat_id:', telegramId); console.log('channel1:', channel1); const isSubscribedToChannel1 = await checkSubscription(telegramId, channel1) if (isSubscribedToChannel1) { console.log('User is subscribed to both channels') workflow.chan1 = 'yes' workflow.chan2 = 'yes' } else { console.log('User is not subscribed to both channels') workflow.chan1 = 'no' workflow.chan2 = 'no' } async function checkSubscription(telegramId, channel) { const url =
https://api.telegram.org/bot${bot.telegramToken}/getChatMember?chat_id=${chatId}&user_id=${telegramId}
const response = await axios.get(url) const status = _.get(response, 'data.result.status') return status === 'member' || status === 'creator' }

https://cdn.discordapp.com/attachments/1139081666862788658/1139220692320133120/image.png

https://cdn.discordapp.com/attachments/1139081666862788658/1139220692819263519/image.png

@bright-magazine-792 @early-train-33247
e
hello @victorious-dawn-38776, to get the user telegram id, you should use
event.tags.conversation["telegram:id"]
, this is correct
But I think it's failing because you are running this code from the Emulator, so there is no telegram id, and also your are not checking if the current channel is Telegram before trying to read the tag. Add an expression with the code
event.channel === "telegram"
and goes to a node where you manage Telegram users, and add another expression with the code
true
that manages all other cases.
b
did it work?
21 Views