googlechat.js
v0.1.0
Published
Google Chat client for Node.js — spaces, DMs, threads, search, presence and real-time events with plain cookie auth (no browser extension). Includes incoming-webhook helpers.
Maintainers
Readme
googlechat.js
A Google Chat client for Node.js, in the spirit of youtubei.js: talk to the same internal endpoints the Chat web app uses — spaces, DMs, threads, search, unreads, presence, attachments and real-time events — from any ordinary project. You only need the cookies of a logged-in Google session. No browser extension, no headless browser, no local server.
Unofficial and not affiliated with Google. The endpoints are internal and can change without notice. Use an account you are willing to risk; automation may violate Google's terms.
npm install ./googlechat.js # or: npm pack → npm install googlechat.js-0.1.0.tgz
npm run build # when installing from sourceESM, Node.js ≥ 20.3. From CommonJS: const { GoogleChat } = await import('googlechat.js').
Quick start
import { GoogleChat } from 'googlechat.js';
const chat = await GoogleChat.create({ cookie: process.env.GOOGLE_COOKIE });
const spaces = await chat.spaces.list();
const messages = await chat.messages.list(spaces[0].id, { limit: 20 });
await chat.messages.send(spaces[0].id, 'hello!');
await chat.messages.reply(spaces[0].id, messages[0].topic_id, 'in-thread reply');Getting the cookies
- Sign in at https://chat.google.com in your browser.
- Export the cookies of
google.com— any of these formats works ascookie:- the
Cookierequest header (DevTools → Network → anychat.google.comrequest → copy value), - a Cookie-Editor / EditThisCookie JSON export,
- the contents of a Netscape
cookies.txt, - a plain object
{ SID, HSID, SSID, OSID, … }.
- the
- Required:
SID,HSID,SSID(andOSID, which is set once you've opened chat.google.com).
Treat cookies like a password. Store them in an env var or secret manager, never in git.
Options
GoogleChat.create({
cookie, // required
cacheFile: '.gchat-auth.json', // persist the XSRF token between runs (or pass `store`)
fetch: myFetch, // custom fetch (proxy, retry, tests)
userAgent, logLevel: 'warn',
lazy: true, // don't authenticate until first call
});If the session cookies expire you'll get Auth failed … cookies are invalid or expired; export fresh ones.
API
| | |
|---|---|
| chat.me | signed-in user |
| chat.spaces.list() / get(id) / find(q) / items() | spaces + DMs, unread-aware sidebar |
| chat.messages.list(id, {limit, since, until}) / topics(id) | message history (newest first) |
| chat.messages.send(id, text) / reply(id, topicId, text) / sendImage(id, text, path, topicId?) | posting |
| chat.threads.get(id, topicId) | full thread |
| chat.dms.list() / get(dmId) / threads(dmId) | direct messages |
| chat.search(query, options) | global search |
| chat.unreads.summary() / badges() / markRead(id) / markUnread(id) / markThreadRead() / markAllRead() | read state |
| chat.presence.get(userIds) / getOne(id) / dms() | presence |
| chat.attachments.url(token) / download(token) | files & images |
| chat.listen() | real-time events (below) |
| chat.client | the low-level GoogleChatClient (every endpoint, raw results) |
since / until accept epoch ms or strings like "7d", "2h".
Real-time events
const live = await chat.listen(); // { subscribe: 'all' | [ids] | false, keepOnline: true }
live.on('message', (e) => console.log(e.groupId?.id, e.body?.message?.text));
live.on('typing', (e) => …); // also: readReceipt, userStatus, reaction,
live.on('error', (err) => …); // membershipChanged, groupUpdated, connect, disconnect
live.stop();Incoming webhooks & Chat-app events (official API)
Separate from the cookie client; no login needed. Also importable as googlechat.js/webhook.
import { GoogleChatWebhook, cards, createChatEventHandler } from 'googlechat.js/webhook';
const hook = new GoogleChatWebhook(process.env.GCHAT_WEBHOOK_URL);
await hook.sendText('*Deploy finished* <users/all>');
await hook.reply('deploy-123', 'same thread'); // threadKey
await hook.sendCard(cards.card({ header: { title: 'Build' }, sections: [cards.section('Result', [cards.textParagraph('ok')])] }));
// receive messages in a Chat app HTTP endpoint (verifies Google's bearer JWT)
http.createServer(createChatEventHandler(async (e) => e.type === 'MESSAGE' ? `You said: ${e.message?.text}` : undefined,
{ verify: { audience: process.env.PROJECT_NUMBER } })).listen(8080);Retries on 429/5xx, URL validation, auto-split of long text (sendLong), classic and Workspace-add-on
payloads. For add-on endpoints verify with { audience: '<endpoint URL>', issuer: 'https://accounts.google.com', certsUrl: GOOGLE_JWKS_URL, email: '<service account>' }.
Notes
- Dependencies: only
protobufjs(loaded lazily when the first request is encoded). - Derived from the
gchatCLI project; the Chrome-extension bridge, CLI and HTTP server were removed. - Image upload and a few endpoints were previously only reachable through the extension; they now use direct requests with your cookies and are the least battle-tested part.
Tests
npm test — cookie parsing, authentication/caching (mock fetch), webhook and event-handler suites.
MIT
