Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions auth.mjs → auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import qrcode from 'qrcode-terminal'
import fs from 'fs'
const qrcode = require('qrcode-terminal')
const fs = require('fs')

// path where the session data will be stored
const SESSION_FILE_PATH = './session.json';

// load the session data if it has been previously saved
export function loadPreviousSession() {
function loadPreviousSession() {
if (fs.existsSync(SESSION_FILE_PATH)) {
let raw = fs.readFileSync('./session.json')
let data = JSON.parse(raw)
Expand All @@ -15,7 +15,7 @@ export function loadPreviousSession() {
}

// save session values to the file upon successful auth
export function startCurrentSession(client, sessionData) {
function startCurrentSession(client, sessionData) {
// generate and scan this code with your phone
client.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
Expand All @@ -33,4 +33,7 @@ export function startCurrentSession(client, sessionData) {
});

client.initialize();
}
}

exports.loadPreviousSession = loadPreviousSession
exports.startCurrentSession = startCurrentSession
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import axios from 'axios'
import fs from 'fs'
import whatsapp from 'whatsapp-web.js'
import express from 'express'
import { loadPreviousSession, startCurrentSession } from './auth.mjs'
import { parseBody } from './parse.mjs'
import { startServer, pingServer } from './ping.mjs'
const axios = require('axios')
const whatsapp = require('whatsapp-web.js')
const { loadPreviousSession, startCurrentSession } = require('./auth.js')
const { parseBody } = require('./parse.js')
const { startServer, pingServer } = require('./ping.js')

// constants
const sessionData = loadPreviousSession()
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "0.0.8",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
Expand Down
9 changes: 6 additions & 3 deletions parse.mjs → parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function remove(substring) {

String.prototype.remove = remove;

export function parseArgs(params) {
function parseArgs(params) {
const pattern = /([^=]+)[\s]*=[\s]*\'([^']+)\'[\s]*(,)?/
let matches = params.match(pattern)

Expand Down Expand Up @@ -33,7 +33,7 @@ export function parseArgs(params) {
}


export async function parseBody(messageObject, callback) {
async function parseBody(messageObject, callback) {
const pattern = /(#|!)([^(\s]+)(\([^)]+\))?/;
let lowerMessageBody = messageObject.body.toLowerCase();
let matches = lowerMessageBody.match(pattern);
Expand Down Expand Up @@ -68,4 +68,7 @@ export async function parseBody(messageObject, callback) {

await callback(parsedData);
}
}
}

exports.parseArgs = parseArgs
exports.parseBody = parseBody
11 changes: 7 additions & 4 deletions ping.mjs → ping.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'
import axios from 'axios'
const express = require('express')
const axios = require('axios')

export function startServer() {
function startServer() {
const app = express()
const port = process.env.PORT || 3000

Expand All @@ -12,10 +12,13 @@ export function startServer() {
}


export function pingServer() {
function pingServer() {
setInterval(() => {
axios.get('http://aliceclient.herokuapp.com/')
.then(res => console.log(res))
.catch(err => console.log(err))
}, 30*1000);
}

exports.startServer = startServer
exports.pingServer = pingServer