Add missing processMessage/processCallback functions for webhook handling
This commit is contained in:
@@ -77,6 +77,31 @@ export async function initBot(config, api, tools, skills) {
|
||||
}
|
||||
}
|
||||
|
||||
// Process incoming message from Telegram
|
||||
async function processMessage(chatId, text, user) {
|
||||
try {
|
||||
const provider = new (await import('../api/index.js')).ZAIProvider(api);
|
||||
const response = await provider.complete(text, { model: 'glm-5.1' });
|
||||
const reply = response.content || '🤖 *zCode*: ' + response;
|
||||
await sendTelegramMessage(chatId, reply);
|
||||
} catch (error) {
|
||||
logger.error('Error processing message:', error.message);
|
||||
await sendTelegramMessage(chatId, '❌ Sorry, an error occurred while processing your message.');
|
||||
}
|
||||
}
|
||||
|
||||
// Process callback query
|
||||
async function processCallback(chatId, data) {
|
||||
try {
|
||||
const provider = new (await import('../api/index.js')).ZAIProvider(api);
|
||||
const response = await provider.complete(data, { model: 'glm-5.1' });
|
||||
const reply = response.content || '🤖 *zCode*: ' + response;
|
||||
await sendTelegramMessage(chatId, reply);
|
||||
} catch (error) {
|
||||
logger.error('Error processing callback:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle webhook
|
||||
// Handle webhook verification (Telegram GET request)
|
||||
app.get('/telegram/webhook', (req, res) => {
|
||||
@@ -126,4 +151,44 @@ export async function initBot(config, api, tools, skills) {
|
||||
res.json({ ok: true });
|
||||
}
|
||||
});
|
||||
|
||||
// Set webhook (if URL provided)
|
||||
async function setWebhook() {
|
||||
const webhookUrl = process.env.ZCODE_WEBHOOK_URL;
|
||||
if (webhookUrl) {
|
||||
const url = `https://api.telegram.org/bot${botToken}/setWebhook`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ url: webhookUrl }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.ok) {
|
||||
logger.info('✓ Webhook set successfully');
|
||||
} else {
|
||||
logger.error('✗ Failed to set webhook:', data.description);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start HTTP server
|
||||
const PORT = process.env.ZCODE_PORT || 3000;
|
||||
httpServer.listen(PORT, () => {
|
||||
logger.info(`✓ HTTP server running on port ${PORT}`);
|
||||
logger.info(`✓ WebSocket server ready`);
|
||||
});
|
||||
|
||||
// Set webhook and keep process alive
|
||||
await setWebhook();
|
||||
|
||||
return {
|
||||
send: sendTelegramMessage,
|
||||
ws: sendWebSocketMessage,
|
||||
waitForMessages: async () => {
|
||||
// Keep process alive
|
||||
await new Promise(() => {});
|
||||
},
|
||||
getConnections: () => connections.size,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user