kykint

Implement '/t(ranslate)' command

Usage: /t [Whatever]
       or /translate [Whatever]

- Translate the 'whatever' and show the result in any kind of chatroom.
  Also, if the given '/t' message is a reply to another message,
  translate the reply target message as well.
Showing 1 changed file with 25 additions and 0 deletions
...@@ -112,3 +112,28 @@ bot.onText(/^(?!\/)((.|\n)+)/, (msg, match) => { ...@@ -112,3 +112,28 @@ bot.onText(/^(?!\/)((.|\n)+)/, (msg, match) => {
112 112
113 translate(received_msg, chatId); 113 translate(received_msg, chatId);
114 }); 114 });
115 +
116 +// /t(ranslate) [Whatever]
117 +// Translate the 'whatever' and show the result in any kind of chatroom.
118 +// Also, if the given '/t' message is a reply to another message,
119 +// translate the reply target message as well.
120 +bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => {
121 + const chatId = msg.chat.id;
122 + const chatType = msg.chat.type;
123 + const received_msg = match[3];
124 + // Whether the given '/t' message is a reply to another message
125 + const isReply = msg.reply_to_message != undefined;
126 + // Whether a message has been given after '/t' command
127 + const msgExists = received_msg != undefined;
128 +
129 + // Translate the reply's target message
130 + if (isReply) {
131 + const replyMsg = msg.reply_to_message.text;
132 + translate(replyMsg, chatId);
133 + }
134 +
135 + // Translate the message after '/t' if exists
136 + if (msgExists) {
137 + translate(received_msg, chatId);
138 + }
139 +});
......