0.1.0 • Published 9 months ago

@titanabrian/chatdaddy-client v0.1.0

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

ChatDaddy Typescript Client

Typescript client for interacting with all ChatDaddy services. You can use this client to authenticate, send & receive messages, update chats, create groups & everything you expect from the ChatDaddy APIs.

API Docs

You can find the full API docs for the service here

Installing the Client

Using NPM:

npm i git+https://github.com/chatdaddy/typescript-client

Using yarn:

yarn add git+https://github.com/chatdaddy/typescript-client

You can then import in your code like:

import { MessagesApi } from '@chatdaddy/client'

Refresh Tokens and Generating Them

We recommend you use refresh tokens to generate these short lived access tokens. The refresh token is immune to password changes & prevents you from ever entering the password in plaintext. The refresh token automatically becomes invalid after 14 Days of inactivity.

You do have to use your password to generate a refresh token.

import { OAuthApi, encodeSHA256 } from '@chatdaddy/client'

const getRefreshToken = async() => {
	const oAuthApi = new OAuthApi()
	const {
      data: { refresh_token },
    } = await oAuthApi.tokenPost({
      phoneNumber: 85212345678, // use your own ChatDaddy registered phone number
	  password: encodeSHA256('plaintextPassword'), // pass the SHA encoded password
      returnRefreshToken: true,
    })
	return refresh_token
}
console.log(getRefreshToken()) // prints something like "676be3ff-8d6e-4e74-8b0a-16e769d1ee80"

Generating Access Tokens and Using Them

All of ChatDaddy's APIs rely on a single access point using the short lived JWT access token. The access token's schema can be read about here.

Presently, all access tokens last for 1 hour.

This SDK includes functions to easily generate and persist access tokens from refresh tokens

import { makeAccessTokenFactory, MessagesApi } from '@chatdaddy/client'
// create a factory that takes care of auto-renewing access tokens when they expire
const getToken = makeAccessTokenFactory({
	request: {
		refreshToken: '676be3ff-8d6e-4e74-8b0a-16e769d1ee80', // example, use your own refresh token
		scopes: ['MESSAGES_SEND_TO_ALL'] // only add scopes to send messages
	},
})
;(async() => {
	// enter the team ID you want to generate the token for
	// read the section below to see how to get your team ID
	const { token } = await getToken('976bf2fe-ar6e-4e74-8b0a-16e769d1ee80')
	console.log(token)
	// above line would print something like "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"

	// send using WA API
	const config = new Configuration(
		{ accessToken: token },
	)
	const messagesApi = new MessagesApi(config)
	const { data: messages } = await messagesApi.messagesPost({
		// use a random channel (API term -- account) in your team
		accountId: 'random',
		// send to phone number
		chatId: '1234667772',
		messageCompose: {
			text: 'Hello from API',
		}
	})
})()

Finding Out your Team ID

  1. Login & open ChatDaddy App from https://app.chatdaddy.tech
  2. Navigate to https://app.chatdaddy.tech/settings/api
  3. Copy team ID from there

Example: example

Examples

The library has a list of examples of how to use this client, you can find them here.

Running the examples

  1. Clone this repository
  2. Run yarn or npm i (whichever package you prefer)
  3. Create a .env file in the repository folder with the following params:

    REFRESH_TOKEN=<chatdaddy refresh token>
    TEAM_ID=<chatdaddy team id>

    You can get this information from the API page on ChatDaddy.

  4. Run example scripts using yarn ts-node examples/{example-script}

    • Eg. yarn ts-node examples/send-message
0.1.0

9 months ago