Package rhymen/go-whatsapp implements the WhatsApp Web API to provide a clean interface for developers

go-whatsapp

Package rhymen/go-whatsapp implements the WhatsApp Web API to provide a clean interface for developers. Big thanks to all contributors of the sigalor/whatsapp-web-reveng project. The official WhatsApp Business API was released in August 2018. You can check it out here.

Installation

go get github.com/Rhymen/go-whatsapp

Usage

Creating a connection

import (
    whatsapp "github.com/Rhymen/go-whatsapp"
)

wac, err := whatsapp.NewConn(20 * time.Second)

The duration passed to the NewConn function is used to timeout login requests. If you have a bad internet connection use a higher timeout value. This function only creates a websocket connection, it does not handle authentication.

Login

qrChan := make(chan string)
go func() {
    fmt.Printf("qr code: %v\n", <-qrChan)
    //show qr code or save it somewhere to scan
}()
sess, err := wac.Login(qrChan)

The authentication process requires you to scan the qr code, that is send through the channel, with the device you are using whatsapp on. The session struct that is returned can be saved and used to restore the login without scanning the qr code again. The qr code has a ttl of 20 seconds and the login function throws a timeout err if the time has passed or any other request fails.

Restore

newSess, err := wac.RestoreWithSession(sess)

The restore function needs a valid session and returns the new session that was created.

Add message handlers

type myHandler struct{}

func (myHandler) HandleError(err error) {
	fmt.Fprintf(os.Stderr, "%v", err)
}

func (myHandler) HandleTextMessage(message whatsapp.TextMessage) {
	fmt.Println(message)
}

func (myHandler) HandleImageMessage(message whatsapp.ImageMessage) {
	fmt.Println(message)
}

func (myHandler) HandleDocumentMessage(message whatsapp.DocumentMessage) {
	fmt.Println(message)
}

func (myHandler) HandleVideoMessage(message whatsapp.VideoMessage) {
	fmt.Println(message)
}

func (myHandler) HandleAudioMessage(message whatsapp.AudioMessage){	
	fmt.Println(message)
}

func (myHandler) HandleJsonMessage(message string) {
	fmt.Println(message)
}

func (myHandler) HandleContactMessage(message whatsapp.ContactMessage) {
	fmt.Println(message)
}

func (myHandler) HandleBatteryMessage(msg whatsapp.BatteryMessage) {
	fmt.Println(message)
}

func (myHandler) HandleNewContact(contact whatsapp.Contact) {
	fmt.Println(contact)
}

wac.AddHandler(myHandler{})

The message handlers are all optional, you don't need to implement anything but the error handler to implement the interface. The ImageMessage, VideoMessage, AudioMessage and DocumentMessage provide a Download function to get the media data.

Sending text messages

text := whatsapp.TextMessage{
    Info: whatsapp.MessageInfo{
        RemoteJid: "[email protected]",
    },
    Text: "Hello Whatsapp",
}

err := wac.Send(text)

Sending Contact Messages

contactMessage := whatsapp.ContactMessage{
			Info: whatsapp.MessageInfo{ 
                RemoteJid: "[email protected]", 
                },
			DisplayName: "Luke Skylwallker",
			Vcard: "BEGIN:VCARD\nVERSION:3.0\nN:Skyllwalker;Luke;;\nFN:Luke Skywallker\nitem1.TEL;waid=0123456789:+1 23 456789789\nitem1.X-ABLabel:Mobile\nEND:VCARD",
		}

id, error := client.WaConn.Send(contactMessage)

The message will be send over the websocket. The attributes seen above are the required ones. All other relevant attributes (id, timestamp, fromMe, status) are set if they are missing in the struct. For the time being we only support text messages, but other types are planned for the near future.

Legal

This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by WhatsApp or any of its affiliates or subsidiaries. This is an independent and unofficial software. Use at your own risk.

License

The MIT License (MIT)

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Owner
Relldev メイ
A newbie
Relldev メイ
Similar Resources

Utilcanvas is a package which provides some utilities to interface with the Canvas LMS Api.

Utilcanvas Utilcanvas is a package which provides some utilities to interface with the Canvas LMS Api. Example usage c := utilcanvas.NewClient("https:

Dec 21, 2022

This package provides a Go interface for the Typeform API.

typeform This package provides a Go interface for the Typeform API

Dec 4, 2022

This project implements a Go client library for the Hipchat API.

Hipchat This project implements a Go client library for the Hipchat API (API version 2 is not supported). Pull requests are welcome as the API is limi

Jan 3, 2023

Go library for accessing trending repositories and developers at Github.

Go library for accessing trending repositories and developers at Github.

go-trending A package to retrieve trending repositories and developers from Github written in golang. This package were inspired by rochefort/git-tren

Dec 21, 2022

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

Dec 20, 2022

Fedora-new-install - Utility scripts for a clean fedora installation

fedora-new-install Utility scripts for a clean fedora installation new-install.g

Nov 8, 2022

Cleanv - Golang SDK for Vue Projects. It is able to structure a clean code/arch pattern

Cleanv - Golang SDK for Vue Projects.  It is able to structure a clean code/arch pattern

Cleanv - Golang SDK for Vue Projects Why? This project is part of my personal portfolio, so, I'll be happy if you could provide me any feedback about

Apr 24, 2022

GoStorm is a Go library that implements the communications protocol required to write Storm spouts and Bolts in Go that communicate with the Storm shells.

gostorm godocs GoStorm is a Go library that implements the communications protocol required for non-Java languages to communicate as part of a storm t

Sep 27, 2022

This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.

observer This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event. Usage go get githu

Nov 16, 2022
Whatsmeow is a Go library for the WhatsApp web multidevice API.

whatsmeow whatsmeow is a Go library for the WhatsApp web multidevice API. This was initially forked from go-whatsapp (MIT license), but large parts of

Dec 27, 2022
WhatsApp Web API

go-whatsapp Package rhymen/go-whatsapp implements the WhatsApp Web API to provide a clean interface for developers. Big thanks to all contributors of

Jan 7, 2023
Clean-todo - An example of go app with clean architecture

clean-todo An example of go app with clean application Functionality This is a s

Jan 26, 2022
Golang API for Whatsapp API MultiDevice version
Golang API for Whatsapp API MultiDevice version

Go Whatsapp API Multi Device Version Required Mac OS: brew install vips export C

Jan 3, 2023
Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)
Simple RESTful API for WhatsApp in Golang (using the Whatsmeow multi device library)

WUZAPI WuzAPI is an implementation of @tulir/whatsmeow library as a simple RESTful API service with multiple device support and concurrent sessions. W

Dec 30, 2022
Godaddy - This package implements the libdns interfaces for the Godaddy API

Godaddy for libdns This package implements the libdns interfaces for the Godaddy

Nov 8, 2022
An unofficial package in Golang to help developers implement ZATCA (Fatoora) QR code easily.

Zatca SDK GO An unofficial package in Golang to help developers to implement ZATCA (Fatoora) QR code easily which required for e-invoicing ✅ The hash

Jan 1, 2023
WhatsAppExpenseTracker - Way to track expenses using whatsapp group

WhatsAppExpenseTracker Way to track expenses using whatsapp group One needs to c

Jan 4, 2022
The Fabric Token SDK is a set of API and services that lets developers create token-based distributed application on Hyperledger Fabric.

The Fabric Token SDK is a set of API and services that let developers create token-based distributed application on Hyperledger Fabric.

Dec 14, 2022
SDK to provide access to JUNO API (Open Banking) (2.0.0)

Juno API - Golang SDK Juno API (Open Banking) (2.0.0) Why? This project is part of my personal portfolio, so, I'll be happy if you could provide me an

Aug 9, 2021