@curatedotfun/telegram
Version:
Telegram plugin for curatedotfun
2 lines • 5.86 kB
JavaScript
;exports.ids=["768"],exports.modules={6747:function(e,t,r){r.r(t),r.d(t,{default:()=>g});var n=r(4820),o=r.n(n);function a(e,t,r,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){r(e);return}u.done?t(c):Promise.resolve(c).then(n,o)}function i(e){return function(){var t=this,r=arguments;return new Promise(function(n,o){var i=e.apply(t,r);function u(e){a(i,n,o,u,c,"next",e)}function c(e){a(i,n,o,u,c,"throw",e)}u(void 0)})}}function u(e,t){if(!(e instanceof t))throw TypeError("Cannot call a class as a function")}function c(e,t,r){return(c=p()?Reflect.construct:function(e,t,r){var n=[null];n.push.apply(n,t);var o=new(Function.bind.apply(e,n));return r&&f(o,r.prototype),o}).apply(null,arguments)}function s(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function l(e){return(l=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function f(e,t){return(f=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function h(e){var t="function"==typeof Map?new Map:void 0;return(h=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return c(e,arguments,l(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),f(r,e)})(e)}function p(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(p=function(){return!!e})()}function d(e,t){var r,n,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(a){return function(u){var c=[a,u];if(r)throw TypeError("Generator is already executing.");for(;i;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===c[0]||2===c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}}}var b=function(e){if("function"!=typeof e&&null!==e)throw TypeError("Super expression must either be null or a function");function t(e,r){var n,o,a;return u(this,t),o=t,a=[e],o=l(o),s(n=function(e,t){var r;if(t&&("object"==((r=t)&&"undefined"!=typeof Symbol&&r.constructor===Symbol?"symbol":typeof r)||"function"==typeof t))return t;if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(this,p()?Reflect.construct(o,a||[],l(this).constructor):o.apply(this,a)),"response",void 0),n.response=r,n.name="TelegramAPIError",n}return t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&f(t,e),t}(h(Error)),g=function(){var e;function t(){u(this,t),s(this,"type","distributor"),s(this,"botToken",null),s(this,"channelId",null),s(this,"messageThreadId",null)}return e=[{key:"initialize",value:function(e){var t=this;return i(function(){var r;return d(this,function(n){switch(n.label){case 0:if(!e)throw Error("Telegram plugin requires configuration.");if(!e.botToken)throw Error("Telegram plugin requires botToken");if(!e.channelId&&!e.messageThreadId)throw Error("Telegram plugin requires either channelId or messageThreadId");if(e.messageThreadId&&!e.channelId)throw Error("Telegram plugin requires channelId when messageThreadId is provided");t.botToken=e.botToken,t.channelId=e.channelId||null,t.messageThreadId=e.messageThreadId||null,n.label=1;case 1:return n.trys.push([1,3,,4]),[4,fetch("https://api.telegram.org/bot".concat(t.botToken,"/getChat?chat_id=").concat(t.channelId||t.messageThreadId))];case 2:if(!n.sent().ok)throw Error("Failed to validate Telegram credentials");return[3,4];case 3:throw console.error("Failed to initialize Telegram plugin:",r=n.sent()),r;case 4:return[2]}})})()}},{key:"distribute",value:function(e){var t=e.input,r=this;return i(function(){var e;return d(this,function(n){switch(n.label){case 0:if(!r.botToken||!r.channelId&&!r.messageThreadId)throw Error("Telegram plugin not initialized");return e=r.formatMessage(t),[4,r.sendMessage(e)];case 1:return n.sent(),[2]}})})()}},{key:"formatMessage",value:function(e){var t=o()(e,"escape");return t.replace(/\]\((https?:\/\/[^)]+)\)/g,function(e,t){return"](".concat(t.replace(/([.-])/g,"\\$1"),")")})}},{key:"sendMessage",value:function(e){var t=this;return i(function(){var r,n,o,a;return d(this,function(i){switch(i.label){case 0:if(!(r=t.channelId))throw Error("No valid chat ID available");return n={chat_id:r,text:e,parse_mode:"MarkdownV2"},t.messageThreadId&&(n.message_thread_id=t.messageThreadId),[4,fetch("https://api.telegram.org/bot".concat(t.botToken,"/sendMessage"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})];case 1:if((o=i.sent()).ok)return[3,3];return[4,o.json()];case 2:throw a=i.sent(),new b("Failed to send message: ".concat(a.description||"Unknown error"),a);case 3:return[2]}})})()}}],function(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}(t.prototype,e),t}()}};
//# sourceMappingURL=768.js.map