skip to Main Content

I try to use telegram bot connect website, when I open the website, I get the query and hash when I validate throw node.js and not equal

const key = crypto.createHash('sha256').update(my_bot_token).digest()
const validateHash = crypto.createHmac('sha256', key).update('auth_date=1646xxxnfirst_name=namexxxnid=1231xxxxnAusername=alexLxxx').digest('hex')
console.log(hash === validateHash) false

I don’t know where I am going wrong I try to use the npm package telegram-checking-authorization, but the same error

2

Answers


  1. You should add all field data you receive from the request callback but remove the hash field.
    Ex:

    `auth_date=1665991955
    first_name=xxxxx
    id=xxxxxx
    last_name=xxxxx
    photo_url=xxxxxxx
    username=xxxxx`
    

    I try and succeed.

    Login or Signup to reply.
  2. It happened to me as well, I described it in My question. You might copy the response from this API: https://oauth.telegram.org/auth/get?bot_id=xxx sometimes the response works with the function, sometimes it doesn’t, and honestly, I don’t understand why. Try to print user to console and check if the two hashes match!

    What I have done is as follow:

    I changed my bot’s domain to https://www.w3schools.com/ and go to w3school online editor https://www.w3schools.com/js/tryit.asp?filename=tryjs_string_indexof for example, print user object to console (replace the javascript in the online editor with code bellow and then replace YOUR-BOT-NAME in the code with the name of your bot):

    <script async src="https://telegram.org/js/telegram-widget.js?21" data-telegram-login="YOUR-BOT-NAME" data-size="large" data-onauth="onTelegramAuth(user)" data-request-access="write"></script>
    <script type="text/javascript">
      function onTelegramAuth(user) {
        console.log(JSON.stringify(user, null, 2));
      }
    </script>
    
    1. press f12, go to console tab and then login with widget. Check the data printed on the console with your function.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search