fix telegram request ipv6 issue (#20)
* fix telegram request ipv6 issue * fix telegram request ipv6 issue * telegram config forceIPv4
This commit is contained in:
parent
a74e11f3f7
commit
41543eb4b9
|
|
@ -69,6 +69,10 @@ TELEGRAM_BOT_TOKEN=your-telegram-bot-token
|
||||||
# Telegram webhook URL(您的公開 HTTPS URL)
|
# Telegram webhook URL(您的公開 HTTPS URL)
|
||||||
# TELEGRAM_WEBHOOK_URL=https://your-domain.com
|
# TELEGRAM_WEBHOOK_URL=https://your-domain.com
|
||||||
|
|
||||||
|
# 強制使用 IPv4 連接 Telegram API(預設:false)
|
||||||
|
# 在某些網路環境下,IPv6 連接可能不穩定,設置為 true 可強制使用 IPv4
|
||||||
|
# TELEGRAM_FORCE_IPV4=false
|
||||||
|
|
||||||
# ===== 系统配置 =====
|
# ===== 系统配置 =====
|
||||||
# 会话映射文件路径
|
# 会话映射文件路径
|
||||||
SESSION_MAP_PATH=/path/to/your/project/src/data/session-map.json
|
SESSION_MAP_PATH=/path/to/your/project/src/data/session-map.json
|
||||||
|
|
|
||||||
16
README.md
16
README.md
|
|
@ -141,6 +141,22 @@ TELEGRAM_WEBHOOK_URL=https://your-ngrok-url.app
|
||||||
SESSION_MAP_PATH=/your/path/to/Claude-Code-Remote/src/data/session-map.json
|
SESSION_MAP_PATH=/your/path/to/Claude-Code-Remote/src/data/session-map.json
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Optional Telegram settings:**
|
||||||
|
```env
|
||||||
|
# Force IPv4 connections to Telegram API (default: false)
|
||||||
|
# Enable this if you experience connectivity issues with IPv6
|
||||||
|
TELEGRAM_FORCE_IPV4=true
|
||||||
|
```
|
||||||
|
|
||||||
|
**Network Configuration Notes:**
|
||||||
|
- **IPv4 vs IPv6**: Some network environments may have unstable IPv6 connectivity to Telegram's API servers
|
||||||
|
- **When to use `TELEGRAM_FORCE_IPV4=true`**:
|
||||||
|
- Connection timeouts or failures when sending messages
|
||||||
|
- Inconsistent webhook delivery
|
||||||
|
- Network environments that don't properly support IPv6
|
||||||
|
- **Default behavior**: Uses system default (usually IPv6 when available, fallback to IPv4)
|
||||||
|
- **Performance impact**: Minimal - only affects initial connection establishment
|
||||||
|
|
||||||
#### Option C: Configure LINE
|
#### Option C: Configure LINE
|
||||||
|
|
||||||
**Required LINE settings:**
|
**Required LINE settings:**
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,8 @@
|
||||||
"botToken": "",
|
"botToken": "",
|
||||||
"chatId": "",
|
"chatId": "",
|
||||||
"groupId": "",
|
"groupId": "",
|
||||||
"whitelist": []
|
"whitelist": [],
|
||||||
|
"forceIPv4": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"whatsapp": {
|
"whatsapp": {
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,18 @@ class TelegramChannel extends NotificationChannel {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate network options for axios requests
|
||||||
|
* @returns {Object} Network options object
|
||||||
|
*/
|
||||||
|
_getNetworkOptions() {
|
||||||
|
const options = {};
|
||||||
|
if (this.config.forceIPv4) {
|
||||||
|
options.family = 4;
|
||||||
|
}
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
_generateToken() {
|
_generateToken() {
|
||||||
// Generate short Token (uppercase letters + numbers, 8 digits)
|
// Generate short Token (uppercase letters + numbers, 8 digits)
|
||||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||||
|
|
@ -73,7 +85,8 @@ class TelegramChannel extends NotificationChannel {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
`${this.apiBaseUrl}/bot${this.config.botToken}/getMe`
|
`${this.apiBaseUrl}/bot${this.config.botToken}/getMe`,
|
||||||
|
this._getNetworkOptions()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.data.ok && response.data.result.username) {
|
if (response.data.ok && response.data.result.username) {
|
||||||
|
|
@ -145,7 +158,8 @@ class TelegramChannel extends NotificationChannel {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${this.apiBaseUrl}/bot${this.config.botToken}/sendMessage`,
|
`${this.apiBaseUrl}/bot${this.config.botToken}/sendMessage`,
|
||||||
requestData
|
requestData,
|
||||||
|
this._getNetworkOptions()
|
||||||
);
|
);
|
||||||
|
|
||||||
this.logger.info(`Telegram message sent successfully, Session: ${sessionId}`);
|
this.logger.info(`Telegram message sent successfully, Session: ${sessionId}`);
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,18 @@ class TelegramWebhookHandler {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate network options for axios requests
|
||||||
|
* @returns {Object} Network options object
|
||||||
|
*/
|
||||||
|
_getNetworkOptions() {
|
||||||
|
const options = {};
|
||||||
|
if (this.config.forceIPv4) {
|
||||||
|
options.family = 4;
|
||||||
|
}
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
async _handleWebhook(req, res) {
|
async _handleWebhook(req, res) {
|
||||||
try {
|
try {
|
||||||
const update = req.body;
|
const update = req.body;
|
||||||
|
|
@ -226,7 +238,8 @@ class TelegramWebhookHandler {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
`${this.apiBaseUrl}/bot${this.config.botToken}/getMe`
|
`${this.apiBaseUrl}/bot${this.config.botToken}/getMe`,
|
||||||
|
this._getNetworkOptions()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.data.ok && response.data.result.username) {
|
if (response.data.ok && response.data.result.username) {
|
||||||
|
|
@ -277,7 +290,8 @@ class TelegramWebhookHandler {
|
||||||
chat_id: chatId,
|
chat_id: chatId,
|
||||||
text: text,
|
text: text,
|
||||||
...options
|
...options
|
||||||
}
|
},
|
||||||
|
this._getNetworkOptions()
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error('Failed to send message:', error.response?.data || error.message);
|
this.logger.error('Failed to send message:', error.response?.data || error.message);
|
||||||
|
|
@ -291,7 +305,8 @@ class TelegramWebhookHandler {
|
||||||
{
|
{
|
||||||
callback_query_id: callbackQueryId,
|
callback_query_id: callbackQueryId,
|
||||||
text: text
|
text: text
|
||||||
}
|
},
|
||||||
|
this._getNetworkOptions()
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error('Failed to answer callback query:', error.response?.data || error.message);
|
this.logger.error('Failed to answer callback query:', error.response?.data || error.message);
|
||||||
|
|
@ -305,7 +320,8 @@ class TelegramWebhookHandler {
|
||||||
{
|
{
|
||||||
url: webhookUrl,
|
url: webhookUrl,
|
||||||
allowed_updates: ['message', 'callback_query']
|
allowed_updates: ['message', 'callback_query']
|
||||||
}
|
},
|
||||||
|
this._getNetworkOptions()
|
||||||
);
|
);
|
||||||
|
|
||||||
this.logger.info('Webhook set successfully:', response.data);
|
this.logger.info('Webhook set successfully:', response.data);
|
||||||
|
|
|
||||||
|
|
@ -96,10 +96,12 @@ class ConfigManager {
|
||||||
},
|
},
|
||||||
telegram: {
|
telegram: {
|
||||||
type: 'chat',
|
type: 'chat',
|
||||||
enabled: false,
|
enabled: process.env.TELEGRAM_ENABLED === 'true',
|
||||||
config: {
|
config: {
|
||||||
token: '',
|
botToken: process.env.TELEGRAM_BOT_TOKEN || '',
|
||||||
chatId: ''
|
chatId: process.env.TELEGRAM_CHAT_ID || '',
|
||||||
|
groupId: process.env.TELEGRAM_GROUP_ID || '',
|
||||||
|
forceIPv4: process.env.TELEGRAM_FORCE_IPV4 === 'true'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue