simpleLogin.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const phoneDiv = document.getElementById('phoneDiv')
  2. const phone = document.getElementById('phone')
  3. const phoneSend = document.getElementById('phoneSend')
  4. const codeDiv = document.getElementById('codeDiv')
  5. const code = document.getElementById('code')
  6. const codeSend = document.getElementById('codeSend')
  7. const passDiv = document.getElementById('passDiv')
  8. const pass = document.getElementById('pass')
  9. const passSend = document.getElementById('passSend')
  10. const logger = document.getElementById('log')
  11. function phoneCallback() {
  12. phone.disabled = false
  13. phoneSend.disabled = false
  14. return new Promise(resolve => {
  15. phoneSend.addEventListener('click', function() {
  16. phone.disabled = true
  17. phoneSend.disabled = true
  18. resolve(phone.value)
  19. })
  20. })
  21. }
  22. function passwordCallback() {
  23. passDiv.style.visibility = 'visible'
  24. return new Promise(resolve => {
  25. passSend.addEventListener('click', function() {
  26. code.disabled = true
  27. codeSend.disabled = true
  28. resolve(pass.value)
  29. alert('welcome')
  30. })
  31. })
  32. }
  33. function codeCallback() {
  34. code.disabled = false
  35. codeSend.disabled = false
  36. codeDiv.style.visibility = 'visible'
  37. return new Promise(resolve => {
  38. codeSend.addEventListener('click', function() {
  39. code.disabled = true
  40. codeSend.disabled = true
  41. resolve(code.value)
  42. })
  43. })
  44. }
  45. const { TelegramClient } = gramjs
  46. const { StringSession } = gramjs.sessions
  47. const apiId = process.env.APP_ID // put your api id here [for example 123456789]
  48. const apiHash = process.env.APP_HASH // put your api hash here [for example '123456abcfghe']
  49. const client = new TelegramClient(new StringSession(''), apiId, apiHash) // you can pass a string session here from previous logins.
  50. // If you want to run this example in the test servers uncomment this line
  51. // client.session.setDC(2, '149.154.167.40', 80)
  52. client.start({
  53. botAuthToken: phoneCallback,
  54. }).then(() => {
  55. console.log('%c you should now be connected', 'color:#B54128')
  56. console.log('%c your string session is ' + client.session.save(), 'color:#B54128')
  57. console.log('%c you can save it to login with it next time', 'color:#B54128')
  58. })