forked from nodegin/tglib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
175 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Changelog | ||
|
||
----- | ||
|
||
#### tglib v1.3 | ||
|
||
- Now support multiple session login by storing files in separate directories. (#16, #19) | ||
- Ability to login as Bots. | ||
```diff | ||
const client = new Client({ | ||
apiId: 'YOUR_API_ID', | ||
apiHash: 'YOUR_API_HASH', | ||
- phoneNumber: 'YOUR_PHONE_NUMBER', | ||
+ auth: { | ||
+ type: 'user', | ||
+ value: 'YOUR_PHONE_NUMBER', | ||
+ }, | ||
}) | ||
``` | ||
- Error events for `client.fetch` are now handled. `ClientFetchError` will be thrown if `client.fetch` fails. | ||
- `client.tg.sendMessageTypeText` renamed to `client.tg.sendTextMessage`. | ||
- `client.connect()` changed to `client.ready`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
class InvalidEventError extends Error { | ||
constructor (eventName) { | ||
constructor(eventName) { | ||
super() | ||
this.message = `"${eventName}" is not a valid event name.` | ||
} | ||
} | ||
|
||
class ClientCreateError extends Error { | ||
constructor (error) { | ||
constructor(error) { | ||
super(error) | ||
this.message = `Unable to create client: ${error.message}` | ||
} | ||
} | ||
|
||
class ClientNotCreatedError extends Error { | ||
constructor (error) { | ||
constructor(error) { | ||
super(error) | ||
this.message = `Client is not created.` | ||
} | ||
} | ||
|
||
class ClientFetchError extends Error { | ||
constructor(update) { | ||
super() | ||
this.message = JSON.stringify(update) | ||
} | ||
} | ||
|
||
module.exports = { | ||
InvalidEventError, | ||
ClientCreateError, | ||
ClientNotCreatedError, | ||
ClientFetchError, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { Client } = require('tglib') | ||
|
||
void async function() { | ||
const client = new Client({ | ||
apiId: 'YOUR_API_ID', | ||
apiHash: 'YOUR_API_HASH', | ||
auth: { | ||
type: 'bot', | ||
value: 'YOUR_BOT_TOKEN', | ||
}, | ||
}) | ||
|
||
await client.ready | ||
|
||
const { id: myId } = await client.fetch({ '@type': 'getMe' }) | ||
|
||
client.on('_update', async (update) => { | ||
if (update['@type'] === 'updateNewMessage') { | ||
// check if message is sent from self | ||
const sender = update['message']['sender_user_id'] | ||
if (sender !== myId) { | ||
const { text: { text } } = update['message']['content'] | ||
if (text.startsWith('/')) { | ||
await client.tg.sendTextMessage(sender, `Are you requested "${text}"?`) | ||
} else { | ||
await client.tg.sendTextMessage(sender, `Sorry I do not understand "${text}".`) | ||
} | ||
} | ||
} | ||
}) | ||
}() |
Oops, something went wrong.