1const Apify = require('apify');
2const moment = require('moment');
3
4const { log } = Apify.utils;
5const IS_TEST_MODE = process.env.IS_TEST_MODE === 'true';
6const TEST_MESSAGE_DATA = 'ToCountry=US&ToState=NJ&SmsMessageSid=SMba0d9db1db81dda8f486e5cd53015502&NumMedia=0&ToCity=HACKENSACK&FromZip=16915&SmsSid=SMba0d9db1db81&FromState=PA&SmsStatus=received&FromCity=GENESEE&Body=589665+is+your+activation+code.&FromCountry=US&To=%2B12015842345&ToZip=07930&NumSegments=1&MessageSid=SMba0d9db1db81dda8f48&AccountSid=ACd7d487f65588e45a5sdsds&From=%2B18142223452&ApiVersion=2010-04-01';
7
8Apify.main(async () => {
9 log.info('Getting message data from input');
10 const messageDataAsString = IS_TEST_MODE
11 ? TEST_MESSAGE_DATA
12 : (await Apify.getInput()).toString();
13
14 const messageData = {};
15 for (const [k, v] of new URLSearchParams(messageDataAsString).entries()) {
16 messageData[k] = v;
17 }
18 messageData.receivedAt = moment.utc().valueOf();
19
20 const phoneNbre = messageData.To.replace('+', '00');
21
22 const sharedKvs = await Apify.openKeyValueStore('twilio-sms-parser');
23
24 log.info(`Saving message data for phone number "${phoneNbre}"`);
25 await sharedKvs.setValue(phoneNbre, messageData);
26});