adityajat/SaarvamAI_botPublic · Bot Template

ProfileTelegram

AISarvam AI is a multi‑purpose Telegram bot that lets users ask questions, get explanations, convert text to speech, and translate messages using the Sarvam AI API. It also offers group administration commands such as warn, mute, ban, pin, delete, and announcements, and can clear personal or group AI memory. The bot uses the TeleBotHost TBL framework with Bot, Api, db, and HTTP calls, and provides inline keyboards for language and voice selection.

Ai Assistantaivoicetranslatemoderationadminsarvam
34 commands4 envUpdated 15d agoCreated Sep 14, 2026
Back to folder

commands/_explain.js

javascript · 145 lines

Raw
1/**#command2name: /explain3answer: 4keyboard: 5parse_mode: markdown6aliases: 7allow_only_group: false8need_reply: false9is_web: 010#command**/11 12let replied = update.message && update.message.reply_to_message;13 14if (!replied || !replied.text) {15  Api.sendMessage({16    text:17      "❌ Please reply to a text message with /explain.\n\n" +18      "💡 Example:\n" +19      "Reply to any message → /explain"20  });21  return;22}23 24let key = process.env.SARVAM_API_KEY;25 26if (!key) {27  Api.sendMessage({28    text: "❌ SARVAM_API_KEY was not found."29  });30  return;31}32 33let prompt =34  "Explain the following message in very humanly like simple language.\n\n" +35 36  "IMPORTANT FORMATTING RULES:\n" +37  "1. Make the answer visually organized.\n" +38  "2. Use emojis as bullet points.\n" +39  "3. Use short sections.\n" +40  "4. Do not write one huge paragraph.\n" +41  "5. Do not use Markdown asterisks (*) or underscores (_).\n" +42  "6. Do not use headings with #.\n" +43  "7. Keep each point short and clear.\n\n" +44  "8. give realistic humanoid answers .\n\n"  +45  "9. use headings with emojis. \n" +46  "Use this structure:\n\n" +47 48  "📌 Main idea\n" +49  "<one or two simple sentences>\n\n" +50 51  "🔹 In simple words\n" +52  "<easy explanation>\n\n" +53 54  "🔹 Key points\n" +55  "• <point 1>\n" +56  "• <point 2>\n" +57  "• <point 3>\n\n" +58 59  "💡 Example\n" +60  "<simple example if useful>\n\n" +61 62  "MESSAGE TO EXPLAIN:\n" +63  replied.text;64 65let res;66 67try {68  res = await HTTP.post(69    "https://api.sarvam.ai/v1/chat/completions",70    {71      headers: {72        "api-subscription-key": key73      },74      body: {75        model: "sarvam-105b",76        messages: [77          {78            role: "system",79            content:80              "reply according to human nature. " +81              "use harsh and slang words where you think should be used." +82              "Always organize answers into summarised sections and bullet points."83          },84          {85            role: "user",86            content: prompt87          }88        ],89        max_tokens: 700,90        reasoning_effort: null91      },92      responseType: "human",93      timeout: 2000094    }95  );96} catch (e) {97  Api.sendMessage({98    text:99      "❌ Couldn't process the message.\n\n" +100      "Please try again."101  });102  return;103}104 105if (!res.ok) {106  Api.sendMessage({107    text:108      "❌ Explanation failed\n\n" +109      "🔹 API error: " +110      res.status111  });112  return;113}114 115let answer;116 117try {118  answer = res.data.choices[0].message.content;119} catch (e) {120  Api.sendMessage({121    text: "❌ Unexpected response from Sarvam."122  });123  return;124}125 126if (!answer || !answer.trim()) {127  Api.sendMessage({128    text: "❌ No explanation was generated."129  });130  return;131}132 133/* Remove unwanted Markdown symbols */134answer = answer135  .replace(/\*/g, "")136  .replace(/__/g, "")137  .replace(/_/g, "")138  .replace(/`/g, "")139  .trim();140 141Api.sendMessage({142  text:143    "💡 EXPLANATION\n\n" +144    answer145});