I want to figure out the format of telegram bot tokens to implement some validity checks, but there seems not official format description.
from my token and what I have found on the net, I can assume the following:
- (up to) 46 characters length
- starts with (up to) 10 digits followed by :
- the remaining 35 characters are of class [[:alnum:]] plus – and _
can anyone (dis) confirm or point to documentation?
3
Answers
Let me summarize what we know so far:
getMe
API call, e.g. on command line:curl -s https://api.telegram.org/botYOURTOKEN/getMe
Nevertheless, we have some good guesses what a correct token must look like:
:
:
is followed by a 35 character Telegram internal identifier/hash[[:alnum:]]
plus_-
, this fit's the characters documented for the deep linking parameterSummary:
Token format:
8-10 digits:35 alnum characters plus _-
, e.g.123456789:AaZz0...AaZz9
Regex for testing:
/^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$/
If you want to check validity of a bot’s token you can use the
getMe
method.https://core.telegram.org/bots/api#getme
Any non valid token will return 401 error.
I believe this would be a more robust approach than checking for correct formats.
The
BOT token
consists of two parts. InBOT_ID:BOT_ALPHANUMERIC_PART
theBOT_ID
is 8 to 10 digit long and theBOT_ALPHANUMERIC_PART
has a length of 35 characters. So the total length is 43 to 45 characters.If you want to validate a bot token then you can use:
https://api.telegram.org/bot< YOUR_BOT_TOKEN>/getMe
.It will return the JSON data for your bot. It will throw a 401 error if the bot token is not valid.