mtcute/docs/guide/advanced/session-convert.md
alina sireneva 690948b8b1
All checks were successful
Build and deploy typedoc / build (push) Successful in 5m15s
chore: moved docs inside the main repo
Co-authored-by: Kamilla 'ova <me@kamillaova.dev>
Co-authored-by: Alina Chebakova <chebakov05@gmail.com>
Co-authored-by: Kravets <57632712+kravetsone@users.noreply.github.com>
Co-authored-by: starkow <hello@starkow.dev>
Co-authored-by: sireneva <150665887+sireneva@users.noreply.github.com>
2025-01-17 08:50:35 +03:00

118 lines
2.8 KiB
Markdown

# Converting sessions
If you're coming from another library, you might already have a session
lying around.
mtcute provides a way to convert sessions from some other libraries
to mtcute's format in `@mtcute/convert` package.
::: warning
Please, **only use this to convert your own sessions**.
**DO NOT** use this to convert stolen sessions or sessions you don't own.
Please be a decent person.
:::
## [Telethon v1.x](https://github.com/LonamiWebs/Telethon)
> Telethon v2 seems to have removed the ability to export sessions,
> so it's currently not supported
```ts
import { convertFromTelethonSession } from '@mtcute/convert'
const client = new TelegramClient({ ... })
await client.importSession(convertFromTelethonSession("..."))
```
## [Pyrogram](https://github.com/pyrogram/pyrogram)
```ts
import { convertFromPyrogramSession } from '@mtcute/convert'
const client = new TelegramClient({ ... })
await client.importSession(convertFromPyrogramSession("..."))
```
## [GramJS](https://github.com/gram-js/gramjs)
```ts
import { convertFromGramjsSession } from '@mtcute/convert'
const client = new TelegramClient({ ... })
await client.importSession(convertFromGramjsSession("..."))
```
## [MTKruto](https://github.com/MTKruto/MTKruto)
```ts
import { convertFromMtkrutoSession } from '@mtcute/convert'
const client = new TelegramClient({ ... })
await client.importSession(convertFromMtkrutoSession("..."))
```
## Backwards
If you need to convert a session from mtcute to another library, you can use the `convertTo*` functions instead:
```ts
console.log(convertToTelethonSession(await client.exportSession()))
```
## String-to-string
Once converted, you can use `writeStringSession` to convert the session to a string:
```ts
console.log(writeStringSession(convertFromTelethonSession("...")))
```
## Manual
If your library is not supported, you can still convert the session manually.
In the most simple case, you'll only need `auth_key` and data center information:
```ts
const dc = {
id: 2,
ipAddress: '149.154.167.41',
port: 443,
}
await client.importSession({
version: 3,
testMode: false,
primaryDcs: { main: dc, media: dc },
authKey: new Uint8Array([ /* ... */ ]),
})
```
### Data center information
If you only know DC ID and not the IP address, you can use the mappings from `@mtcute/convert` to resolve it:
```ts
import { DC_MAPPING_PROD } from '@mtcute/convert'
const dc = DC_MAPPING_PROD[2]
```
If you don't know such information at all, you can just always use the DC 2 (as above), and mtcute will handle the rest
### User information
If you happen to know some information about the user logged in, it might help to provide it as well:
```ts
await client.importSession({
...,
self: {
userId: 777000,
isBot: false,
isPremium: false,
usernames: [],
}
})
```