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CAEI,GAAU,EAAO,MAAQ,EAAO,UAEhC,QAAQ,KAAK,CACT,IAAK,WAAa,EAAgB,4BAA8B,EAAY,aAAe,EAAO,SAClG,QAAS,CACL,cAAiB,UAAY,EAC7B,eAAgB,uBAEpB,SAAU,CACN,MAAO,EAAO,KACd,WAAY,KAEjB,SAAU,EAAO,EAAU,GAC1B,GAAI,EACA,MAAM,EACH,CACH,EAAO,KAAK,MAAM,GAClB,IAAI,EAAW,GAEf,IAAK,IAAI,KAAK,EAAK,WACf,EAAS,GAAK,EAAK,WAAW,GAAG,GAGrC,IAAI,EAAQ,CAAC,EACT,GAAU,EAAO,YACjB,EAAmB,YAAI,CAAC,CACpB,UAAa,EAAO,aAG5B,QAAQ,KAAK,CACT,IAAK,WAAa,EAAgB,gBAClC,QAAS,CACL,cAAiB,UAAY,GAEjC,KAAM,CACF,QAAS,EACT,WAAY,EACZ,SAAU,EACV,MAAO,GAEX,MAAM,IACP,SAAU,EAAK,EAAK,GACnB,GAAI,EAAK,MAAM,CACnB,GACJ,CACJ,GAER,CAjGgB,IAAZ,EACA,QAAQ,KAAK,CACT,IAAK,WAAa,EAAgB,sBAClC,KAAM,CACF,SAAU,EACV,SAAU,GAEd,MAAM,IACP,SAAU,EAAO,EAAU,GAC1B,GAAI,EAAO,MAAM,EACjB,WAAW,KAAK,CACZ,OAAU,EACV,SAAY,EACZ,MAAS,EAAS,QAAQ,QAE9B,EAAW,EAAS,QAAQ,MAE5B,GACJ,IAGA,GA6ER,CA1OA,OAAO,QAAU,WACjB,OAAO,QAAQ,QAAU,QACzB,OAAO,QAAQ,cAAgB","file":"/npm/mattermost-api@1.0.1/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/mattermost-api@1.0.1/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict'\nconst request = require('request');\nvar config, userConfig = [];\n\nmodule.exports = mattermost\nmodule.exports.sendmsg = sendmsg\nmodule.exports.sendmsgByUser = sendmsgByUser\n\n\n/**\n * function for send error to mattermost\n * @param object of url and username \n */\nfunction mattermost(option) {\n    config = JSON.parse(JSON.stringify(option));\n    // webhook url of mattermost\n    if (!option.url) throw new Error('URL required');\n    // username which displays on mattermost\n    if (!option.username) throw new Error('username required');\n    return function mattermost(err, req, res, next) {\n        try {\n            let message = \"#### Error occurred in \" + option.username + \".\\n\";\n            message += \"Requested URL : \" + req.protocol + \"://\" + req.subdomains + req.hostname + req.originalUrl + \"\\n\";\n            message += \"Requested Method : \" + req.method + \"\\n\";\n            message += \"res.headersSent  : \" + res.headersSent + \"\\n\";\n            message += \"Body : \\n \" + JSON.stringify(req.body) + \"\\n\";\n            message += \"Query String : \\n \" + JSON.stringify(req.query) + \" \\n\";\n            message += \"Stack : \\n ```\\n \" + err.stack + \" \\n``` \\n\";\n            // post request for send message to mattermost\n            let body = {\n                text: message,\n                username: option.username\n            }\n            if (option.icon_url) {\n                body['icon_url'] = option.icon_url;\n            }\n            request.post({\n                url: option.url,\n                body: body,\n                json: true\n            }, function (error, response, body) {\n                if (error) throw error;\n            });\n        } catch (e) {\n            throw e;\n        }\n        next();\n    }\n}\n\n/**\n * function for send custom message to mattermost\n * @param string message which sent to mattermost\n * @param object of url and username\n */\nfunction sendmsg(message, option = null) {\n    let url, username;\n    if (!config && !option) throw new Error('required object of url and username');\n    if (!message) throw new Error('message required');\n    if (config) {\n        url = config.url;\n        username = config.username;\n    }\n    if (option != null && option.url) {\n        url = option.url;\n    }\n    if (option != null && option.username) {\n        username = option.username;\n    }\n    try {\n        // post request for send message to mattermost\n        let body = {\n            text: message,\n            username: username\n        }\n        if (config && config.icon_url) {\n            body['icon_url'] = config.icon_url;\n        }\n        if (option != null && option.icon_url) {\n            body['icon_url'] = option.icon_url;\n        }\n        if (option && option.image_url) {\n            body['attachments'] = [{\n                \"image_url\": option.image_url\n            }];\n        }\n        request.post({\n            url: url,\n            body: body,\n            json: true\n        }, function (error, response, body) {\n            if (error) throw error;\n        });\n    } catch (e) {\n        throw e;\n    }\n}\n\n\n/**\n * @param {object} option object contains userid, password, mattermost URL, message, channelid \n */\nfunction sendmsgByUser(message, option) {\n    let apiToken = '',\n        userid, password, mattermosturl, channelid;\n    if (!message) throw new Error('required message');\n    if (!config.userid && !option.userid) throw new Error('required userid');\n    if (!config.password && !option.password) throw new Error('required password');\n    if (!config.mattermosturl && !option.mattermosturl) throw new Error('required mattermost URL');\n    if (!config.channelid && !option.channelid) throw new Error('required channelid');\n\n    // set required parameter\n    if (option && option.userid) {\n        userid = option.userid;\n    } else {\n        userid = config.userid;\n    }\n    if (option && option.password) {\n        password = option.password;\n    } else {\n        password = config.password;\n    }\n    if (option && option.mattermosturl) {\n        mattermosturl = option.mattermosturl;\n    } else {\n        mattermosturl = config.mattermosturl;\n    }\n    if (option && option.channelid) {\n        channelid = option.channelid;\n    } else {\n        channelid = config.channelid;\n    }\n    // if user aleredy loggedin then use earlier token \n    for (let inx in userConfig) {\n        if (userConfig[inx].userid == userid) {\n            apiToken = userConfig[inx].token;\n            break;\n        }\n    }\n    // if user is not loggedin then call api for login and set user's info in userConfig array\n    if (apiToken == '') {\n        request.post({\n            url: 'https://' + mattermosturl + '/api/v4/users/login',\n            body: {\n                login_id: userid,\n                password: password\n            },\n            json: true\n        }, function (error, response, body) {\n            if (error) throw error;\n            userConfig.push({\n                'userid': userid,\n                'password': password,\n                'token': response.headers.token\n            });\n            apiToken = response.headers.token;\n            // function for sent message\n            msgsent();\n        });\n    } else {\n        // if user alerady logged in then sent message with old token\n        msgsent();\n    }\n\n    //function for send message with optional file parameter\n    function msgsent() {\n        // if user send only message info\n        if (!option || (option && !option.file && !option.filename)) {\n            let props = {};\n            if (option && option.image_url) {\n                props['attachments'] = [{\n                    \"image_url\": option.image_url\n                }];\n            }\n            request.post({\n                url: 'https://' + mattermosturl + '/api/v4/posts',\n                headers: {\n                    'Authorization': 'Bearer ' + apiToken\n                },\n                body: {\n                    message: message,\n                    channel_id: channelid,\n                    props: props\n                },\n                json: true\n            }, function (error, response, body) {\n                if (error) throw error;\n            });\n        }\n        // if user send message with file\n        if (option && option.file && option.filename) {\n            // api for upload file\n            request.post({\n                url: 'https://' + mattermosturl + '/api/v4/files?channel_id=' + channelid + '&filename=' + option.filename,\n                headers: {\n                    'Authorization': 'Bearer ' + apiToken,\n                    \"Content-Type\": \"multipart/form-data\"\n                },\n                formData: {\n                    files: option.file,\n                    channel_id: channelid\n                },\n            }, function (error, response, body) {\n                if (error) {\n                    throw error\n                } else {\n                    body = JSON.parse(body);\n                    let file_ids = [];\n                    // set upload files id to create post api\n                    for (let i in body.file_infos) {\n                        file_ids[i] = body.file_infos[i].id;\n                    }\n                    // api for create post(message)\n                    let props = {};\n                    if (option && option.image_url) {\n                        props['attachments'] = [{\n                            \"image_url\": option.image_url\n                        }];\n                    }\n                    request.post({\n                        url: 'https://' + mattermosturl + '/api/v4/posts',\n                        headers: {\n                            'Authorization': 'Bearer ' + apiToken\n                        },\n                        body: {\n                            message: message,\n                            channel_id: channelid,\n                            file_ids: file_ids,\n                            props: props\n                        },\n                        json: true\n                    }, function (err, res, body) {\n                        if (err) throw err;\n                    });\n                }\n            });\n        }\n    }\n};"]}