mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge 2ddf5166e1
into b81fdd71da
This commit is contained in:
commit
12c946e017
@ -222,15 +222,21 @@ export class ChatService {
|
|||||||
throw new Error(`Subscriber with foreign ID ${foreignId} not found`);
|
throw new Error(`Subscriber with foreign ID ${foreignId} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const sentMessage: MessageCreateDto = {
|
const mid = event.getId();
|
||||||
mid: event.getId(),
|
const message = await this.findMessageWithRetries(mid);
|
||||||
recipient: recipient.id,
|
debugger;
|
||||||
message: event.getMessage(),
|
|
||||||
delivery: true,
|
|
||||||
read: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
this.eventEmitter.emit('hook:chatbot:sent', sentMessage);
|
if (!message) {
|
||||||
|
const sentMessage: MessageCreateDto = {
|
||||||
|
mid,
|
||||||
|
recipient: recipient.id,
|
||||||
|
message: event.getMessage(),
|
||||||
|
delivery: true,
|
||||||
|
read: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.eventEmitter.emit('hook:chatbot:sent', sentMessage);
|
||||||
|
}
|
||||||
this.eventEmitter.emit('hook:stats:entry', 'echo', 'Echo');
|
this.eventEmitter.emit('hook:stats:entry', 'echo', 'Echo');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error('Unable to log echo message', err, event);
|
this.logger.error('Unable to log echo message', err, event);
|
||||||
@ -238,6 +244,40 @@ export class ChatService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retries finding a message by mid with exponential backoff.
|
||||||
|
*
|
||||||
|
* @param mid - The message ID.
|
||||||
|
* @param maxRetries - Max number of retries (default: 5).
|
||||||
|
* @param initialDelay - Initial delay in ms (default: 100).
|
||||||
|
* @returns True if the message is found, false otherwise.
|
||||||
|
*/
|
||||||
|
async findMessageWithRetries(
|
||||||
|
mid: string,
|
||||||
|
maxRetries = 5,
|
||||||
|
initialDelay = 100,
|
||||||
|
) {
|
||||||
|
let attempt = 0;
|
||||||
|
let delay = initialDelay;
|
||||||
|
|
||||||
|
while (attempt < maxRetries) {
|
||||||
|
const exists = await this.messageService.count({ mid });
|
||||||
|
|
||||||
|
if (exists > 0) {
|
||||||
|
return true; // Message exists
|
||||||
|
}
|
||||||
|
|
||||||
|
attempt++;
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||||
|
delay *= 2; // Exponential backoff
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.debug(
|
||||||
|
'Echo message not found after multiple attempts. Treating as an external echo.',
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle new incoming messages
|
* Handle new incoming messages
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user