/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@interrep/telegram-bot@0.6.0/dist/index.node.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
/**
 * @module @interrep/telegram-bot
 * @version 0.6.0
 * @file InterRep Telegram bot to allow users to join Semaphore Telegram groups.
 * @copyright Omar Desogus 2022
 * @license MIT
 * @see [Github]{@link https://github.com/InterRep/interrep.js/tree/main/packages/telegram-bot}
*/
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var TelegramGroup,db=require("@interrep/db"),grammy=require("grammy"),jsSha256=require("js-sha256");
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(e,t,r,n){return new(r||(r=Promise))((function(o,a){function s(e){try{u(n.next(e))}catch(e){a(e)}}function i(e){try{u(n.throw(e))}catch(e){a(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,i)}u((n=n.apply(e,t||[])).next())}))}function __generator(e,t){var r,n,o,a,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(a){return function(i){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&a[0]?n.return:a[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,a[1])).done)return o;switch(n=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,n=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){s.label=a[1];break}if(6===a[0]&&s.label<o[1]){s.label=o[1],o=a;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(a);break}o[2]&&s.ops.pop(),s.trys.pop();continue}a=t.call(e,s)}catch(e){a=[6,e],n=0}finally{r=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,i])}}}!function(e){e.INTERREP="-1001396261340"}(TelegramGroup||(TelegramGroup={}));var TelegramGroup$1=TelegramGroup;function getTelegramGroups(){return Object.values(TelegramGroup$1)}function sha256(e){var t=jsSha256.sha256.create();return t.update(e),t.hex()}function showConnectButton(e,t,r,n){return __awaiter(this,void 0,void 0,(function(){var o;return __generator(this,(function(a){switch(a.label){case 0:return o=(new grammy.InlineKeyboard).url("Connect","https://telegram.me/"+e.botInfo.username+"?start=connect"),[4,e.api.sendMessage(t.id,"Click below, start the bot in a private chat and run /"+n+" again to receive InterRep magic links!",{reply_to_message_id:r.message_id,reply_markup:o})];case 1:return a.sent(),[2]}}))}))}function join(e,t,r,n,o){return __awaiter(this,void 0,void 0,(function(){var a,s,i,u;return __generator(this,(function(c){switch(c.label){case 0:if("private"===r.type&&e.api.sendMessage(r.id,"You cannot use this command in a private chat. Please run the /help command to see how the bot works!"),"private"===r.type||!o)return[3,14];c.label=1;case 1:return c.trys.push([1,10,,14]),a=r.id.toString(),getTelegramGroups().includes(a)?[3,3]:[4,e.api.sendMessage(o.id,"Sorry, the '"+r.title+"' group is not supported, please contact the InterRep team if you want to add this group to the supported ones.")];case 2:return c.sent(),[2];case 3:return s=sha256(o.id.toString()+r.id.toString()),[4,db.TelegramUser.findByHashId(s)];case 4:return(i=c.sent())&&i.hasJoined?[4,e.api.sendMessage(o.id,"You already joined the '"+r.title+"' Semaphore group!")]:[3,6];case 5:return c.sent(),[2];case 6:return i?[3,8]:[4,db.TelegramUser.create({hashId:s,hasJoined:!1})];case 7:c.sent(),c.label=8;case 8:return[4,e.api.sendMessage(o.id,"Here's the magic link to join the '"+r.title+"' group: "+t+"/groups/telegram/"+o.id+"/"+r.id+" 😉")];case 9:return c.sent(),[3,14];case 10:return 403!==(null==(u=c.sent())?void 0:u.error_code)?[3,12]:[4,showConnectButton(e,r,n,"join")];case 11:return c.sent(),[3,13];case 12:console.error(u),c.label=13;case 13:return[3,14];case 14:return[2]}}))}))}function leave(e,t,r,n,o){return __awaiter(this,void 0,void 0,(function(){var a,s,i,u;return __generator(this,(function(c){switch(c.label){case 0:if("private"===r.type&&e.api.sendMessage(r.id,"You cannot use this command in a private chat. Please run the /help command to see how the bot works!"),"private"===r.type||!o)return[3,12];c.label=1;case 1:return c.trys.push([1,8,,12]),a=r.id.toString(),getTelegramGroups().includes(a)?[3,3]:[4,e.api.sendMessage(o.id,"Sorry, this group is not supported, please contact the InterRep team!")];case 2:return c.sent(),[2];case 3:return s=sha256(o.id.toString()+r.id.toString()),[4,db.TelegramUser.findByHashId(s)];case 4:return(i=c.sent())&&i.hasJoined?[3,6]:[4,e.api.sendMessage(o.id,"You are not part of the '"+r.title+"' Semaphore group!")];case 5:return c.sent(),[2];case 6:return[4,e.api.sendMessage(o.id,"Here's the magic link to leave the '"+r.title+"' group: "+t+"/groups/telegram/"+o.id+"/"+r.id+" 😉")];case 7:return c.sent(),[3,12];case 8:return 403!==(null==(u=c.sent())?void 0:u.error_code)?[3,10]:[4,showConnectButton(e,r,n,"leave")];case 9:return c.sent(),[3,11];case 10:console.error(u),c.label=11;case 11:return[3,12];case 12:return[2]}}))}))}function sendHelpMessage(e,t,r){return __awaiter(this,void 0,void 0,(function(){var n,o;return __generator(this,(function(a){switch(a.label){case 0:return"private"===t.type?(n=r.first_name,o="Add me to a supported group here on Telegram so I can send a magic link to anyone who wants to join the corresponding Semaphore group\\. You can see our supported groups [here](https://docs.interrep.link/technical-overview/groups/telegram)\\."):(n="everyone",o="Run /join so I can redirect you to our application where you will able to join the `"+t.title+"` Semaphore group\\. If you want to leave a group run /leave\\."),[4,e.api.sendMessage(t.id,"Hi `"+n+"` 👋 If you want a system that allows you to access services or functions without revealing your identity, you are in the right place\\. InterRep provides special groups which [Semaphore](https://semaphore.appliedzkp.org/) then uses to create completely anonymous proofs of membership\\.\n\n"+o+"\n\nIf you want to know more about InterRep, visit our [documentation website](https://docs.interrep.link) and our [Github repositories](https://github.com/interrep)\\.",{parse_mode:"MarkdownV2",disable_web_page_preview:!0})];case 1:return a.sent(),[2]}}))}))}function sendStartMessage(e,t,r,n){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(o){return"private"===t.type&&("connect"===r?e.api.sendMessage(t.id,"Great, you can receive the magic links here now 🎉\n\nRun /join when you are in a group!"):sendHelpMessage(e,t,n)),[2]}))}))}var InterRepBot=function(){function e(e,t,r){var n=this;this.bot=new grammy.Bot(e),this.mongodbUrl=t,this.appURL=r,this.bot.api.setMyCommands([{command:"help",description:"show the help message"},{command:"join",description:"send you a magic link to join a group"},{command:"leave",description:"send you a magic link to leave a group"}]),this.bot.command("help",(function(e){return sendHelpMessage(n.bot,e.chat,e.from)})),this.bot.command("start",(function(e){return sendStartMessage(n.bot,e.chat,e.match,e.from)})),this.bot.command("join",(function(e){return __awaiter(n,void 0,void 0,(function(){return __generator(this,(function(t){return[2,join(this.bot,this.appURL,e.chat,e.msg,e.from)]}))}))})),this.bot.command("leave",(function(e){return __awaiter(n,void 0,void 0,(function(){return __generator(this,(function(t){return[2,leave(this.bot,this.appURL,e.chat,e.msg,e.from)]}))}))}))}return e.prototype.start=function(e){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(t){switch(t.label){case 0:return[4,db.connect(this.mongodbUrl)];case 1:return t.sent(),[4,this.bot.start(e)];case 2:return t.sent(),[2]}}))}))},e}();exports.InterRepBot=InterRepBot,exports.TelegramGroup=TelegramGroup$1,exports.getTelegramGroups=getTelegramGroups;
//# sourceMappingURL=/sm/1add1c02f0801492cf40b655907febbc1aeaa770eae7887ce25f3a9d441aba12.map