diff --git a/README.md b/README.md index 6bf7c21..5b7facd 100755 --- a/README.md +++ b/README.md @@ -59,7 +59,20 @@ After the user has send a message to your bot the first time, you can gather the curl -X GET https://api.telegram.org/bot{YOUR_TELEGRAM_TOKEN}/getUpdates ``` -A more detailed list of instructions can be found here [https://core.telegram.org/bots#botfather](https://core.telegram.org/bots#botfather) +A more detailed list of instructions can be found here [https://core.telegram.org/bots#botfather](https://core.telegram.org/bots#botfather) + +##### SendGrid +```json +"sendGrid": { + "apiKey": "", + "from": "", + "receiver": "", + "templateId": "", + "enabled": true +} +``` +If you don't use Slack, I'd personally recommend using Sendgrid. It's a free service (free as in "you cannot send more than 100 emails a day"), which is more than enough for Fredy. + ### 2. Multiple search jobs diff --git a/conf/config.example b/conf/config.example index d121ada..38b835d 100755 --- a/conf/config.example +++ b/conf/config.example @@ -13,6 +13,13 @@ "token": "", "enabled": false }, + "sendGrid": { + "apiKey": "", + "from": "", + "receiver": "", + "templateId": "", + "enabled": false + }, "console": { "enabled": true } diff --git a/doc/sendgrid-email-template.hbs b/doc/sendgrid-email-template.hbs new file mode 100644 index 0000000..321002a --- /dev/null +++ b/doc/sendgrid-email-template.hbs @@ -0,0 +1,235 @@ + + + + + + + + + + + + + + +
+
+ + + + +
+ + + + +
+ + +
+ + + + + + +
+ + + + +
+
+
+ + + +

Service {{serviceName}} found {{numberOfListings}} new listing(s)!

+ + + + + +
+
+ + + + {{#each listings}} + + + + {{/each}} + +
+
+ {{this.title}} +
+ Size: {{this.size}} +
+ {{this.address}} +
+ {{this.link}} +
+ --------------------------- +
+
+ + + + + +
+
+ + + + + +
+ + + + +
+
+ + + + +
+
+ + + + + + + +
+ + + + +
+ + Powered by Fredy + +
+
+ + + +
+
+ +
+ + + + + + +
+
+ + + \ No newline at end of file diff --git a/lib/notification/adapter/sendGrid.js b/lib/notification/adapter/sendGrid.js new file mode 100755 index 0000000..99844dd --- /dev/null +++ b/lib/notification/adapter/sendGrid.js @@ -0,0 +1,28 @@ +const sgMail = require('@sendgrid/mail'); + +/** + * sends a new listing using SendGrid + * @param serviceName e.g immoscout + * @param newListings an array with newly found listings + * @param notificationConfig config of this notification adapter + * @returns {Promise | void} + */ +exports.send = (serviceName, newListings, notificationConfig) => { + const { apiKey, enabled, receiver, from, templateId } = notificationConfig.sendGrid; + if (!enabled) { + return [Promise.resolve()]; + } + sgMail.setApiKey(apiKey); + const msg = { + templateId, + to: receiver, + from, + subject: `Service ${serviceName} found ${newListings.length} new listing(s)`, + dynamic_template_data: { + serviceName, + numberOfListings: newListings.length, + listings: newListings, + }, + }; + return sgMail.send(msg); +}; diff --git a/package.json b/package.json index 4ece29f..59c3e94 100755 --- a/package.json +++ b/package.json @@ -43,17 +43,18 @@ "npm": ">=6.0.0" }, "dependencies": { + "@sendgrid/mail": "^7.2.1", "body-parser": "1.19.0", "lowdb": "1.0.0", "request-x-ray": "0.1.4", - "restana": "4.6.1", + "restana": "4.6.2", "slack": "11.0.2", "tg-yarl": "1.3.0", "x-ray": "2.3.4" }, "devDependencies": { "chai": "4.2.0", - "eslint": "7.3.0", + "eslint": "7.4.0", "eslint-config-prettier": "6.11.0", "husky": "4.2.5", "lint-staged": "10.2.11", diff --git a/yarn.lock b/yarn.lock index f316b89..b854bc9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -33,6 +33,30 @@ dependencies: regenerator-runtime "^0.13.2" +"@sendgrid/client@^7.2.1": + version "7.2.1" + resolved "https://registry.yarnpkg.com/@sendgrid/client/-/client-7.2.1.tgz#9d631a3d2ed44f06a08dee9730da7755eb72fb3c" + integrity sha512-QizSa+qKnq5xquMyeB2x0l2JNH3jvg3zHmGt0Ghz+DLUGpW8i1m/zvrqSZ0wdcGVO4/2lW3WFN0drv1+jOxVgA== + dependencies: + "@sendgrid/helpers" "^7.2.0" + axios "^0.19.2" + +"@sendgrid/helpers@^7.2.0": + version "7.2.0" + resolved "https://registry.yarnpkg.com/@sendgrid/helpers/-/helpers-7.2.0.tgz#75cbde76815fa0f8b7966b3e2a0650fc125c3276" + integrity sha512-Ps8rc3xFpaQvIWoW1zlWi9G7QnxYwXFOeq0915DzXf5knoiOgXdFk/XBXbCFjr2lf/JIzjgFUtAV/dN8Z8jzHw== + dependencies: + chalk "^2.0.1" + deepmerge "^4.2.2" + +"@sendgrid/mail@^7.2.1": + version "7.2.1" + resolved "https://registry.yarnpkg.com/@sendgrid/mail/-/mail-7.2.1.tgz#4e1d9cb84406a2b8ed6db3ddf8cba9d17e6badd0" + integrity sha512-LBAdwL+c6HmN2B/Q24LFy2do/IzBFeFoF05xBqgELxp8lMUwis4fbqgo09Zyj1CY0qHN+CfWcUfLNvHfVCh8oQ== + dependencies: + "@sendgrid/client" "^7.2.1" + "@sendgrid/helpers" "^7.2.0" + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" @@ -201,6 +225,13 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.9.1.tgz#7e33d8f7d449b3f673cd72deb9abdc552dbe528e" integrity sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug== +axios@^0.19.2: + version "0.19.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27" + integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA== + dependencies: + follow-redirects "1.5.10" + balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" @@ -301,7 +332,7 @@ chai@4.2.0: pathval "^1.1.0" type-detect "^4.0.5" -chalk@^2.0.0, chalk@^2.4.2, chalk@~2.4.0: +chalk@^2.0.0, chalk@^2.0.1, chalk@^2.4.2, chalk@~2.4.0: version "2.4.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ== @@ -537,6 +568,13 @@ debug@3.2.6, debug@^3.1.0: dependencies: ms "^2.1.1" +debug@=3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" + integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== + dependencies: + ms "2.0.0" + debug@^4.0.1, debug@^4.1.1, debug@~4.1.0: version "4.1.1" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" @@ -566,6 +604,11 @@ deep-is@^0.1.3: resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34" integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ= +deepmerge@^4.2.2: + version "4.2.2" + resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955" + integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg== + define-properties@^1.1.2, define-properties@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1" @@ -835,10 +878,10 @@ eslint-visitor-keys@^1.2.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.2.0.tgz#74415ac884874495f78ec2a97349525344c981fa" integrity sha512-WFb4ihckKil6hu3Dp798xdzSfddwKKU3+nGniKF6HfeW6OLd2OUDEPP7TcHtB5+QXOKg2s6B2DaMPE1Nn/kxKQ== -eslint@7.3.0: - version "7.3.0" - resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.3.0.tgz#f9f1fc3dc1227985d0db88769f2bbac7b4b875d7" - integrity sha512-dJMVXwfU5PT1cj2Nv2VPPrKahKTGdX+5Dh0Q3YuKt+Y2UhdL2YbzsVaBMyG9HC0tBismlv/r1+eZqs6SMIV38Q== +eslint@7.4.0: + version "7.4.0" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.4.0.tgz#4e35a2697e6c1972f9d6ef2b690ad319f80f206f" + integrity sha512-gU+lxhlPHu45H3JkEGgYhWhkR9wLHHEXC9FbWFnTlEkbKyZKWgWRLgf61E8zWmBuI6g5xKBph9ltg3NtZMVF8g== dependencies: "@babel/code-frame" "^7.0.0" ajv "^6.10.0" @@ -1042,6 +1085,13 @@ flatted@^2.0.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08" integrity sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg== +follow-redirects@1.5.10: + version "1.5.10" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" + integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ== + dependencies: + debug "=3.1.0" + forever-agent@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" @@ -2300,10 +2350,10 @@ resolve@^1.11.1: dependencies: path-parse "^1.0.6" -restana@4.6.1: - version "4.6.1" - resolved "https://registry.yarnpkg.com/restana/-/restana-4.6.1.tgz#a8d43b76b7f36e5e8bde74ca110fe252c64ddc44" - integrity sha512-639EiBwn3jCbeNp2Ga1ppXmnZBEFRjqHwv/+jL6hbgjKYTjZuHlbOZq2aIgahzBrh8cBwUDlaWWRO/NH3lor0A== +restana@4.6.2: + version "4.6.2" + resolved "https://registry.yarnpkg.com/restana/-/restana-4.6.2.tgz#665713b6f413c4901425a8c6550bdeb27468e99a" + integrity sha512-jkMmsYblRSCobP1/5FOFrPNrwtvZi4ErdZ0QTcLcM1azNtiy+3oPThYz3WNTfkbCs8TmqsUIYg2wtuP3ANZCqg== dependencies: "0http" "^2.5.2"