Retrieve telegram chat_id
# 🤝help
b
I am trying to get the Telegram Chat id on the bot but I can't get it for nothing. Funny thing is that I can see it on the logs [telegram] Sending message to chat 2084279680: Welcome to BOT NAME! Is it possible to get this value 2084279680 somehow?
b
@early-train-33247 is this possible?
e
Hello! If you are referring to the user id you can read it at
event.conversation.tags["telegram:id"]
b
I tried on the execute Action and on the Before Incoming message hooks, and both failed @early-train-33247

https://cdn.discordapp.com/attachments/1138910991728509029/1138948290189197324/Screenshot_2023-08-09_183228.png

https://cdn.discordapp.com/attachments/1138910991728509029/1138948290432487525/Screenshot_2023-08-09_183244.png

Here is the logs: [telegram] Received message from user 2084279680: oi Hooks An error occurred on hook before_incoming_middleware "Before Message": TypeError: Cannot read properties of undefined (reading 'tags') [TypeError, Cannot read properties of undefined (reading 'tags')] DM Executing hook conversation_turn_started of agent "SummaryAgent"
The log from the Execute Action DM Execute code "get telegram chat id and save on the variable chat_id" Actions Telegram id =====: Actions Error executing action "inline-ins-d7b7c54bf7.js" in flow:Main:node:Standard1 [TypeError, Cannot read properties of undefined (reading 'tags')] Dialog [341fc290-b8aa-466a-8823-56c07935be51] Entering workflow (flow:Error) [node:Entry] from (flow:Main) [node:Standard1]
e
Hey @bitter-lamp-30479 I'm sorry I got it the other way around. It's actually
event.tags.conversation["telegram:id"]
v
Hey! Have you find a solution?
b
Got it @early-train-33247 and @victorious-dawn-38776 Working perfectly. One last question @early-train-33247 . Is there a way for me to list all this tags? or a documentation that I could lean on?
v
@bitter-lamp-30479 can you share your full code, please? I'm trying to create a script that will check if user is subscribed to my telegram channel.
b
I am not doing much @victorious-dawn-38776 , just checking the user to see if he/she already selected the language.

https://cdn.discordapp.com/attachments/1138910991728509029/1139164964783935629/image.png

console.log('Telegram id =====: ') console.log(event.tags.conversation['telegram:id'])

https://cdn.discordapp.com/attachments/1138910991728509029/1139165124054229012/image.png

v
Do you use any webhook?
b
No not really. I just created one to check if the variable I wanted was there.

https://cdn.discordapp.com/attachments/1138910991728509029/1139165751144628324/image.png

The code that Guilherme gave us work there as well.
I checked
v
I'm kinda noobie so I'm really struggling with my code. Just trying to collect user's id and compare it with list of my telegram channel but I'm continiously getting errors
So this webhook collects chatId of your bot?
b
Yes.
v
I'm getting an error with the same code for some reason

https://cdn.discordapp.com/attachments/1138910991728509029/1139166734750859304/image.png

b
The code you need is quite complex to be honest
You need to use tables
the error on this code here is the lack of "("
after the if
v
ChatGPT provided me this one: const telegramId = event.preview 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 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' }
b
You are missing and ( here
line 2
v
Still have an issue with 'chat'

https://cdn.discordapp.com/attachments/1138910991728509029/1139168381732405299/image.png

I have to create a chat variable, right?
b
yes
and you can use the code Guilherme gave us to get the user id
I don't know how to get the chat_id
But I assumet is always the same number right?
The one on this url that you called channel
https://api.telegram.org/bot${bot.telegramToken}/getChatMember?chat_id=${channel}&user_id=${telegramId}
I assume that every user that send a message to this channel is on your chat.
I don't know much about group chat. sorry
I would remove replace line 04 and 05 for the code.
v
Yeah, I've tryied different variations
I actually would easily do it if it was just a js in my visual studio with telegram bot library, since there are a lot of guides about achieving such functionality
I've add chat variable and I still have this underline for some reason. I've add 'chat' variable to workflow, user and bot
b
For me botpress is working great if I were you I would keep using it and trying to put it to work.
if you have a telegram bot library you can try expose it in a url
and make botpress call it
v
I know, it simplify things. I hope I someone with experienced in this field will be online today so I can ask for help
b
You want quite a complex workflow. I would try hiring someone to implement this for you. I don't think someone helping you in a few minutes would be able to achieve all that.
v
It actually should be a pretty easy code. I just need to collect user's id and compare it to the list of user's id of my telegram group then return 'yes' or 'no' value to my workflow.chan1 and chan2 variables
webhook stuff makes it complicated
b
const telegramId = event.tags.conversation['telegram:id'] 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 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' }
does this code works?
v
let me try
this is kinda strange url: https://api.telegram.org/bot$%7Bbot.telegramToken%7D/getChatMember?chat_id=${channel}&user_id=${telegramId}
it underlines red
maybe I should use previous one?
the problem is with bot$ part
b
You gotta read the telegram api documetation
get the correct URL there
chatgpt is just with knowledge from 2021
v
can you drop here a link, please?
b
This looks like the method you want
#getChatMember
v
my bot actually is not a member of the group
it's separate
Would you kindly tell me how can I add my bot API in this link properly, so I can try it? I have a feeling it actually will work
Maybe it already should work, but it show an issue. I will try it anyways.

https://cdn.discordapp.com/attachments/1138910991728509029/1139174486663516250/image.png

b
There is an error on discord
that is corrupting the code I am sending to you
v
yeah, the problem is not the link
b
const url = ``https://api.telegram.org/bot${bot.telegramToken}/getChatMember?chat_id=${channel}&user_id=${telegramId}``
it gotta be between `` this url itself
\` \`
const url = \`https://api.telegram.org/bot${bot.telegramToken}/getChatMember?chat_id=${channel}&user_id=${telegramId}\`
v
yeah
b
now I got it right
v
I'm trying it right now
Same issue

https://cdn.discordapp.com/attachments/1138910991728509029/1139176943393185892/image.png

b
https://api.telegram.org/bot/METHOD_NAME This is how telegram URL should look like as per their documentation
looks like your code is correct
I don't know what is wrong
They changed the URL and put a PROXY on it
Don't know why
v
Actually, my channel1 and channel2 variables collects links to channels which user should be subscribed. I guess it's wrong
b
yeah
try hardcoding the actual value of your channel
v
using @?
or just the name
Actually, I will try both
b
yeah... lol
have no idea
v
I guess I almost found the solution
The only struggle right now is this thing

https://cdn.discordapp.com/attachments/1138910991728509029/1139196980531310612/image.png

just need to get telegram id
b
yeap... that is exactly what you need
looks like you are receving messages from the botpress itself
you need to debug from the telegram chat
you need to publish your bot and start talking to it via telegram
otherwise you will not receive the code
v
I've tried it but there is no result
if only there were a way to get telegram logs
b
you can get the Botpress Logs
v
from telegram?
b
no
v
how then?
b
logs in general
v
where to find them? just the same logs will work?
b

https://cdn.discordapp.com/attachments/1138910991728509029/1139211018820661300/Screenshot_2023-08-10_at_11.57.26.png

You can see the message being received from telegram there
so user_id is working
the problem is the chat_id
this var is wrong
v
what should it be?
b
you have to ge the correct chat_id
It must be on the tags as well
Maybe
event.tags.conversation["telegram:chat_id"]
@early-train-33247 may know the answer for that
v
there is a link for chat id

https://cdn.discordapp.com/attachments/1138910991728509029/1139216098919719054/image.png

I guess it should be just a name
I've changed my chatId variable and I'm getting the same result:

https://cdn.discordapp.com/attachments/1138910991728509029/1139217545539362947/image.png

https://cdn.discordapp.com/attachments/1138910991728509029/1139217545807794217/image.png

b
That escapes my knowledge @victorious-dawn-38776
Haven't worked with chat id
v
I don't think that telegram:chat_id and telegram:id ate capturing into variables as logs in botpress can't collect them and at the same time they are displayed properly in general logs. I don't want to waste your time helping me, just thought it could be useful to you as well just in case you will need something like this in the future

https://cdn.discordapp.com/attachments/1138910991728509029/1139218639694864507/image.png

b
thanks.
Looks like there is a presentation happening at the moment. Maybe Guilhermy will help you after.
e
Hey @bitter-lamp-30479, thanks for helping the community!
@victorious-dawn-38776, the only tag provided by the Telegram integration is
telegram:id
It's available on
event.tags.conversation["telegram:id"]
and on
event.tags.user["telegram:id"]
, it's the same id for conversations and users.
109 Views