• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A package to parse WhatsApp chats with Node.js or in the browser 💬

WhatsApp Chat Parser

Continuous Integration codecov npm version minified size

A package to parse WhatsApp chats with Node.js or in the browser 💬

Important notice

🚨 v4.0.0 brings some BREAKING CHANGES, check out the release page for more info.

Introduction

This library allows you to parse WhatsApp chat logs from text format into javascript objects, enabling you to more easily manipulate the data, create statistics, export it in different formats, etc.

You can test the package online with this example website:
whatsapp-chat-parser.netlify.app (Source code)

Install

$ npm install whatsapp-chat-parser

Usage

Node

import fs from 'node:fs';
import * as whatsapp from 'whatsapp-chat-parser';

const text = fs.readFileSync('path/to/_chat.txt', 'utf8');
const messages = whatsapp.parseString(text);

console.log(messages);

Browser

Add the script to your HTML file (usually just before the closing </body> tag).
Then use it in your JavaScript code, the whatsappChatParser variable will be globally available.

<script src="path/to/index.global.js"></script>
<script>
  const messages = whatsappChatParser.parseString(
    '06/03/2017, 00:45 - Sample User: This is a test message',
  );

  console.log(messages);
</script>

Or with type="module" loading the ESM version:

<script type="module">
  import * as whatsapp from 'path/to/index.js';

  const messages = whatsapp.parseString(
    '06/03/2017, 00:45 - Sample User: This is a test message',
  );

  console.log(messages);
</script>

You can also use the jsDelivr CDN.

<script src="https://cdn.jsdelivr.net/npm/whatsapp-chat-parser/dist/index.global.js"></script>
<!-- Or use a specific version -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.global.js"></script>

Message structure

The messages variable is an array of objects like this:

[
  {
    date: '2018-06-02T22:45:00.000Z', // Date object
    author: 'Luke',
    message: 'Hey how are you?',
  },
  {
    date: '2018-06-02T23:48:00.000Z', // Date object
    author: 'Joe',
    message: 'All good, thanks',
  },
];

When using the option parseAttachments, the message may contain an additional property attachment:

[
  {
    date: '2018-06-02T23:50:00.000Z', // Date object
    author: 'Joe',
    message: '<attached: 00000042-PHOTO-2020-06-07-15-13-20.jpg>',
    attachment: {
      fileName: '00000042-PHOTO-2020-06-07-15-13-20.jpg',
    },
  },
];

In the case of a system message, the author will be null

[
  {
    date: '2018-06-02T22:45:00.000Z', // Date object
    author: null,
    message: 'You created group "Party 🎉"',
  },
];

API

parseString(string, [options]) → Array

string

Type: string

Raw string of the WhatsApp conversation

options

Type: object

A configuration object, more details below

Options

Name Type Default Description
daysFirst Boolean undefined Specify if the dates in your log file start with a day (true) or a month (false). Manually specifying this may improve performance. By default the program will try to infer this information using 3 different methods (look at date.ts for the implementation), if all fails it defaults to days first.
parseAttachments Boolean false Specify if attachments should be parsed. If set to true, messages with attachments will include an attachment property with information about the attachment.

A note about messages order

Sometimes, likely due to connection issues, WhatsApp exports contain messages that are not chronologically ordered.
This library won't change the order of the messages, but if your application expects a certain order make sure to sort the array of messages accordingly before use.

See #247 for more info.

How to export WhatsApp chats

Technologies used

Requirements

Node

Node.js >= 8.0.0

Browser

This package is written in TypeScript with target compilation to ES6.
It should work in all relevant browsers from ~2017 onwards.

Changelog

CHANGELOG

License

MIT

More Repositories

1

whatsapp-chat-parser-website

Website to view your exported WhatsApp chat logs 👁‍🗨
TypeScript
160
star
2

calamity-vscode

A purple dark theme with medium to high contrast for Visual Studio Code ⚛️
33
star
3

genshin-impact-team-randomizer

A web app to randomize your Genshin Impact team based on the characters that you own
TypeScript
22
star
4

calamity-atom

A purple dark theme with medium to high contrast for the Atom editor ⚛️
Less
13
star
5

edabit-js-challenges

Solutions for JavaScript challenges on Edabit 📝
JavaScript
9
star
6

lissajous-curves

Website that draws Lissajous curves on an html canvas 🌀
JavaScript
8
star
7

calamity-sublime

A purple dark theme with medium to high contrast for Sublime Text ⚛️
7
star
8

tlou-fan-website

A fanmade website that pays homage to my favorite video game: The Last of Us 🏹
SCSS
7
star
9

postcss-italian-stylesheets

[DEPRECATED] PostCSS plugin for writing Italian Stylesheets 🇮🇹
JavaScript
5
star
10

loris-portfolio

My personal portfolio website, a place to showcase my projects 👨‍💻
JavaScript
5
star
11

muzik

A music streaming website similar to Spotify (didactic project) 🎶
HTML
4
star
12

rapture-vscode

A dark blue theme with bright accents inspired by the fictional city of Rapture 🌃
3
star
13

tlou2-countdown

A website that shows a countdown to the release of The Last of Us Part II ⏳
JavaScript
3
star
14

fall-guys-perfect-match

A simple webapp to help players with bad memory in the Perfect Match minigame of Fall Guys 🧠
HTML
3
star
15

frontend-mentor-challenges

Solutions for frontend challenges on frontendmentor.io 🗂
SCSS
3
star
16

url-viewer

A simple website to parse and view complex urls 🔗
HTML
2
star
17

youtube-timestamp-scroller

Browser extension to scroll back to a YouTube comment after watching its timestamp 🔃
JavaScript
1
star
18

fiorella-portfolio

A website that showcases my aunt's paintings 🖼
PHP
1
star