telegram-webhook-js
Advanced tools
Comparing version 1.0.3 to 1.0.4
31
main.js
@@ -254,2 +254,33 @@ // telegram-bot.js | ||
} | ||
async getImageFromUpdate(update) { | ||
if (update.message && update.message.photo) { | ||
// Extract the highest resolution photo | ||
const photoArray = update.message.photo; | ||
const fileId = photoArray[photoArray.length - 1].file_id; | ||
// Get file path | ||
const fileInfo = await this.getFile(fileId); | ||
const filePath = fileInfo.file_path; | ||
// Construct the download URL | ||
const fileUrl = `https://api.telegram.org/file/bot${this.token}/${filePath}`; | ||
return fileUrl; // Return the URL to download the image | ||
} else { | ||
console.error('No photo found in update.'); | ||
return null; | ||
} | ||
} | ||
async getFile(fileId) { | ||
try { | ||
const response = await fetch(`${this.apiUrl}getFile?file_id=${fileId}`); | ||
const data = await response.json(); | ||
return data.result; | ||
} catch (error) { | ||
console.error('Error getting file:', error); | ||
} | ||
} | ||
} |
{ | ||
"name": "telegram-webhook-js", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"main": "main.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -5,3 +5,3 @@ # Telegram Webhook JS | ||
This library provides a simplified interface to interact with the Telegram Bot API, enabling you to create, manage, and operate a Telegram bot. It includes methods to send messages, edit messages, handle updates, send various types of media, and manage callback queries for inline buttons. | ||
This library provides a simplified interface to interact with the Telegram Bot API, enabling you to create, manage, and operate a Telegram bot. It includes methods to send messages, edit messages, handle updates, send various types of media, manage callback queries for inline buttons, and handle menu button keyboards. | ||
@@ -192,2 +192,51 @@ ## Installation | ||
### Extracting an Image from Updates | ||
To retrieve an image from an update, you can use the following method in your `TelegramBot` class: | ||
```js | ||
async getImageFromUpdate(update) { | ||
if (update.message && update.message.photo) { | ||
// Extract the highest resolution photo | ||
const photoArray = update.message.photo; | ||
const fileId = photoArray[photoArray.length - 1].file_id; | ||
// Get file path | ||
const fileInfo = await this.getFile(fileId); | ||
const filePath = fileInfo.file_path; | ||
// Construct the download URL | ||
const fileUrl = `https://api.telegram.org/file/bot${this.token}/${filePath}`; | ||
return fileUrl; // Return the URL to download the image | ||
} else { | ||
console.error('No photo found in update.'); | ||
return null; | ||
} | ||
} | ||
async getFile(fileId) { | ||
try { | ||
const response = await fetch(`${this.apiUrl}getFile?file_id=${fileId}`); | ||
const data = await response.json(); | ||
return data.result; | ||
} catch (error) { | ||
console.error('Error getting file:', error); | ||
} | ||
} | ||
``` | ||
You can use this method to handle image updates in your bot: | ||
```js | ||
const updates = await bot.getUpdates(); | ||
for (const update of updates) { | ||
const imageUrl = await bot.getImageFromUpdate(update); | ||
if (imageUrl) { | ||
console.log('Image URL:', imageUrl); | ||
// You can now download the image or process it further | ||
} | ||
} | ||
``` | ||
## API Reference | ||
@@ -300,2 +349,2 @@ | ||
- `callbackData` (string): The callback data to handle. | ||
- `handler` (function): The handler function to execute when the callback data is received. | ||
- `handler` (function): The handler function to execute when the callback data is received. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17032
252
347
12