Skip to content

Commit

Permalink
[feat] typing effect added
Browse files Browse the repository at this point in the history
  • Loading branch information
chisa-dev committed Jan 3, 2025
1 parent 9a4789a commit 02eed21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
43 changes: 28 additions & 15 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ bot.on('callback_query', async (query) => {
}
});

// Function for typing effect
async function sendTypingEffect(bot, chatId, text) {
let message = '';
for (let i = 0; i < text.length; i++) {
message += text[i];
await bot.editMessageText(`<code>${message}</code>\n \n click to copy`, {
chat_id: chatId,
message_id: bot.lastSentMessageId,
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [
[{ text: 'Translate', callback_data: 'translate' }, { text: 'Grammar Fix', callback_data: 'grammar_fix' }],
[{ text: 'Delete', callback_data: `delete:${bot.lastUserMessageId}` }]
]
}
});
await new Promise(res => setTimeout(res, 50));
}
}

// Grammar and Translation
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
Expand All @@ -42,20 +62,14 @@ bot.on('message', async (msg) => {

try {
const correctedText = await correctGrammar(msg.text);

// Store the user's message ID
const userMessageId = msg.message_id;
bot.lastUserMessageId = msg.message_id;

// Send the corrected message with the Delete button
const sentMessage = await bot.sendMessage(chatId, `<code>${correctedText}</code>\n \n click to copy`, {
parse_mode : 'HTML',
reply_markup: {
inline_keyboard: [
[{ text: 'Translate', callback_data: 'translate' }, { text: 'Grammar Fix', callback_data: 'grammar_fix' }],
[{ text: 'Delete', callback_data: `delete:${userMessageId}` }]
]
}
});
// Send initial message to start typing effect
const sentMessage = await bot.sendMessage(chatId, '<code></code>', { parse_mode: 'HTML' });
bot.lastSentMessageId = sentMessage.message_id;

// Apply typing effect
await sendTypingEffect(bot, chatId, correctedText);
} catch (error) {
console.error('Error handling message:', error);
bot.sendMessage(chatId, "An error occurred while processing your message.");
Expand All @@ -75,5 +89,4 @@ bot.on('callback_query', async (query) => {
}
});


console.log("LisanBot is running!");
console.log("LisanBot is running!");
2 changes: 1 addition & 1 deletion helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function correctGrammar(text) {
try {
const completion = await openai.chat.completions.create({
model: "gpt-4",
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: `Fix grammar for: ${text}` }]
});
return completion.choices[0].message.content;
Expand Down

0 comments on commit 02eed21

Please sign in to comment.