Skip to content

Commit

Permalink
feat(notification): add support for Free mobile (#2285)
Browse files Browse the repository at this point in the history
The french carrier Free propose to their customer an option
allowing them to send HTTP GET requests to a gateway that will
send in turn a text SMS to the customer phone.

As the carrier API is purely web, and PHONE_NUMBER is irrelevant here,
two new env parameters have been added:

- FREEMOBILE_ID       (Free mobile account id)
- FREEMOBILE_API_KEY  (API key provided with the option)
  • Loading branch information
aradenac authored Apr 9, 2021
1 parent a25b7f1 commit 6be2e0d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/reference/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,17 @@ You don't need to submit your application for review, just whitelist yourself!
| `STREAMLABS_IMAGE`| Custom image to display. Leave it blank for default |
| `STREAMLABS_SOUND` | Custom image to play. Leave it blank for default |
| `STREAMLABS_DURATION` | StreamLabs alert duration (in milliseconds) |

## Free mobile SMS notifications API

For the customers of Free carrier.

Activate the notification service at mobile.free.fr/account/mes-options (option "Notifications par SMS").
Copy the API key generated with the service activation.

| Environment variable | Description |
|:---:|---|
| `FREEMOBILE_ID` | User ID to log into mobile.free.fr |
| `FREEMOBILE_API_KEY` | API key generated with your notification option activation |

Note: here you do not need to give neither your password nor phone number.
2 changes: 2 additions & 0 deletions dotenv-example
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ STREAMLABS_TYPE=
STREAMLABS_IMAGE=
STREAMLABS_SOUND=
STREAMLABS_DURATION=
FREEMOBILE_ID=
FREEMOBILE_API_KEY=
WEB_PORT=
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ const notifications = {
soundHref: envOrString(process.env.STREAMLABS_SOUND),
duration: envOrNumber(process.env.STREAMLABS_DURATION),
},
freemobile: {
id: envOrString(process.env.FREEMOBILE_ID),
apiKey: envOrString(process.env.FREEMOBILE_API_KEY),
},
};

const nvidia = {
Expand Down
47 changes: 47 additions & 0 deletions src/notification/freemobile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Link, Store} from '../store/model';
import {Print, logger} from '../logger';
import {config} from '../config';
import {URL} from 'url';
import fetch from 'node-fetch';

const {freemobile} = config.notifications;

const url = new URL('https://smsapi.free-mobile.fr/sendmsg');

if (freemobile.id && freemobile.apiKey) {
url.searchParams.append('user', freemobile.id);
url.searchParams.append('pass', freemobile.apiKey);
}

export function sendFreeMobileAlert(link: Link, store: Store) {
if (freemobile.id && freemobile.apiKey) {
logger.debug('↗ sending free mobile alert');

(async () => {
const color = false;
const sms = true;

const message = `${Print.inStock(
link,
store,
color,
sms
)}\n${Print.productInStock(link)}`;

url.searchParams.append('msg', message);

try {
const response = await fetch(url.toString(), {
method: 'GET',
});

const http200Success = await response.ok;
if (!http200Success) throw Error(JSON.stringify(response));

logger.info('✔ free mobile alert sent');
} catch (error: unknown) {
logger.error("✖ couldn't send free mobile alert", error);
}
})();
}
}
2 changes: 2 additions & 0 deletions src/notification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {sendTwitchMessage} from './twitch';
import {updateRedis} from './redis';
import {activateSmartthingsSwitch} from './smartthings';
import {sendStreamLabsAlert} from './streamlabs';
import {sendFreeMobileAlert} from './freemobile';
import {sendApns} from './apns';

export function sendNotification(link: Link, store: Store) {
Expand All @@ -41,4 +42,5 @@ export function sendNotification(link: Link, store: Store) {
sendTwitchMessage(link, store);
updateRedis(link, store);
sendStreamLabsAlert(link, store);
sendFreeMobileAlert(link, store);
}

0 comments on commit 6be2e0d

Please sign in to comment.