UNPKG

@b1kt0p/slack-bot

Version:
2 lines 4.03 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"ReplyError",{enumerable:!0,get:function(){return _utils.ReplyError}}),exports.default=void 0,Object.defineProperty(exports,"format_slack_date",{enumerable:!0,get:function(){return _utils.format_slack_date}});var _url=_interopRequireDefault(require("url")),_https=_interopRequireDefault(require("https")),_moment=_interopRequireDefault(require("moment")),_webApi=require("@slack/web-api"),_message=_interopRequireDefault(require("./message")),_interaction=_interopRequireDefault(require("./interaction")),_utils=require("./utils");function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}class SlackBot{constructor(a={}){this.opt=a,this._client=new _webApi.WebClient(this.opt.bot_token),this._messageListeners=[],this._interactionListeners=[]}async init(){const a=await this._client.auth.test();this.bot={id:a.user_id,bot_id:a.bot_id,name:a.user}}get client(){return this._client}get middleware(){const a=this;return function(b,c){const d=a._proccessEvent(b.body)||{};c.json(d)}}message(a,b,c){const{overhear:d=!1}=c||{};("string"==typeof a||a instanceof RegExp)&&(a=[a]),this._messageListeners.push(async c=>{if((d||c.direct_message||c.direct_mention)&&test_pattern(a,c,c.message)){try{await b(c)}catch(a){console.error(a);const b=a instanceof _utils.ReplyError?a.message:"Something went wrong";await this.replyEphemeral(c,{text:b})}return!0}return!1})}interaction(a,b){("string"==typeof a||a instanceof RegExp)&&(a=[a]),this._interactionListeners.push(async c=>{var d,e,f;const g=null!==(d=null!==(e=c.data.callback_id)&&void 0!==e?e:null===(f=c.data.actions)||void 0===f||null===(f=f[0])||void 0===f?void 0:f.action_id)&&void 0!==d?d:"";if(test_pattern(a,c,g)){try{await b(c)}catch(a){console.error(a);const b=a instanceof _utils.ReplyError?a.message:"Something went wrong";await this.replyEphemeral(c,{text:b})}return!0}return!1})}async reply({event:a},b){return await this._client.chat.postMessage({channel:a.channel,...b})}async updateChat({event:a},b){return await this._client.chat.update({ts:a.event_ts,channel:a.channel,...b})}async replyEphemeral({event:a},b){return await this._client.chat.postEphemeral({channel:a.channel,user:a.user,...b})}async replyThread({event:a},b){return await this._client.chat.postMessage({channel:a.channel,thread_ts:a.ts,...b})}async deleteChat({event:a}){return await this._client.chat.delete({ts:a.event_ts,channel:a.channel})}updateInteraction(a,b){return new Promise((c,d)=>{const e=_url.default.parse(a.data.response_url),f=`payload=${encodeURIComponent(JSON.stringify(b))}`,g={hostname:e.hostname,port:443,path:e.path,method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded","Content-Length":f.length}},h=_https.default.request(g,a=>200===a.statusCode?void c():d(a.statusMessage));h.on("error",a=>d(a)),h.write(f),h.end()})}async proceedEvent(a){if(a.token!==this.opt.verification_token)throw new Error("Failed to verify token");if("url_verification"===a.type)return{challenge:a.challenge};// Workarround to solve issue with lambda duplicated invokes up to 3 times with interval 10 minutes const b=(0,_moment.default)().subtract(5,"minutes");if("event_callback"===a.type){if(a.event.bot_id&&a.event.bot_id===this.bot.bot_id)return{};if(!a.event.text)return{};const c=new _message.default(this.bot,a);if(b.isAfter(c.timestamp))return{};const d=await Promise.all(this._messageListeners.map(a=>a(c)));if((c.direct_message||c.direct_mention)&&!d.includes(!0))return await this.reply(c,{text:"I don`t understand you."})}else if("interactive_message"===a.type||"block_actions"===a.type){const c=new _interaction.default(this.bot,a);if(b.isAfter(c.timestamp))return{};await Promise.all(this._interactionListeners.map(a=>a(c)))}}}exports.default=SlackBot;function test_pattern(a,b,c){for(const d in a){const e=a[d];if("string"==typeof e&&e.toLowerCase()===c.toLowerCase())return!0;if(e instanceof RegExp){const a=c.match(e);if(a)return b.match=a,!0}}return!1}