Comments (10)
You may use Auth.prototype.getState
for checking authorization state:
const { auth, updates } = airgram
airgram.use(auth)
airgram.use(updates)
const getDialogs = () => airgram.client.messages.getDialogs({/* ... */})
auth.getState().then(({ userId }) => {
if (userId) {
return getDialogs()
}
return auth.login().then(getDialogs)
}).catch((error) => {
console.error(error)
})
from airgram.
Well I get what you say, but there is one problem and I have many problems with asynchronous codes 😆 I dont know if it is only my problem but they seem very uncontrollable.
Can you show me where to put this code in the index.ts
file of your airgram-ts-example?
I have problem with that auth.login
thing cause I have used it before in the index.ts
file and I think it caused a double login and made the old stored keys to disappear and I should have removed the database and login again to make the system work again.
Can I ommit the auth.login
part? I mean can we assume that we are logged in by now and then get the state and check its value?
from airgram.
Okay, I will update the example today.
from airgram.
Thanks for the response!
I have another question, How this part of code in the index.ts file know that we are connected to telegram servers and logged in. How does it tell? My question is about the whole asynchronous thing.
// Get dialogs list
airgram.client.messages.getDialogs({
flags: 0,
limit: 30,
offset_date: 0,
offset_id: 0,
offset_peer: { _: 'inputPeerEmpty' }
}).then((dialogs) => {
console.info(dialogs)
}).catch((error) => {
console.error(error)
})
from airgram.
Can I ommit the auth.login part? I mean can we assume that we are logged in by now and then get the state and check its value?
If you use Auth
middleware you don't should not to call auth.login()
because middleware checks whether you are authorized or not (you won't be signed in twice).
Method auth.login()
unregister your previous access keys, so you need to check the current state like:
const { userId } = await auth.getState()
If userId
is defined just skip the login step.
I think it's good idea to create auth.onLogin(): Promise<ag.AuthDoc>
method:
auth.onLogin().then(({ userId }) => {
// your own code
})
from airgram.
Yeah yeah that would be perfect cause when I looked at the index.ts
file I thought to myself that this is whole asynchronous and how can I guarantee that what I want gets executed when I am surely logged in. Thanks for your time!
from airgram.
I have another question, How this part of code in the index.ts file know that we are connected to telegram servers and logged in. How does it tell? My question is about the whole asynchronous thing.
Method airgram.client.messages.getDialogs()
knows nothing about authorization status. The Auth
middleware checks authorization status and calls auth.login()
if it is necessary.
from airgram.
Method airgram.client.messages.getDialogs() knows nothing about authorization status. The Auth middleware checks authorization status and calls auth.login() if it is necessary.
Aha so you mean If I am not logged in, It returns an error, right? So it assumes that we are logged in and it is not fault-tolerant.
from airgram.
Please wait for update. This behavior will be changed.
from airgram.
Now you can freely use auth.login()
. Authorization keys wont be deleted. If user is already signed in, authorization will be skipped.
auth.login().then(async () => {
// Start long polling
await updates.startPolling()
// Get dialogs list
const dialogs = await airgram.client.messages.getDialogs({
flags: 0,
limit: 30,
offset_date: 0,
offset_id: 0,
offset_peer: { _: 'inputPeerEmpty' }
})
console.log(dialogs)
}).catch((error) => {
console.error(error)
})
Full example is here.
from airgram.
Related Issues (20)
- "const airgram = new Airgram();" gives an error on startup
- error {code = 400 message = "Failed to parse JSON object as TDLib request: Can't parse "<bot_id_string>" as an integer"}
- failed authentication telegram when using self signed certificate
- check sending status
- how to import telegram web UI
- how to import telegram web UI
- Valid api_id must be provided. Can be obtained at HOT 10
- Chat info not found
- API object show empty {} and not working any api function
- Airgram JavaScript example not working HOT 1
- TdLib 1.8.6+ requires modified authorization object. HOT 1
- Example/how-to on how to access database (messages)?
- Reactions missing from MessageInteractionInfo
- is posible, create and manage bots with airgram?
- Enable `ignore_background_updates` in Airgram HOT 1
- spoilered message is unsupported
- "@airgram/web": "^5.0.0", cannot send images when running on the browser. No matter what value the Path is set to, it will display [sendMessage][400] Can't find real file path.
- Incompatible architecture [macOS]
- Auth Problem: Cant receive code
- Execute bot command in 1-1 personal chat instead of showing command text
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from airgram.