Skip to content

Commit

Permalink
Release 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppecastaldo committed Sep 14, 2022
1 parent 31435db commit b0ba1c7
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 312 deletions.
12 changes: 12 additions & 0 deletions whatsapp_addon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 1.2.0

- **Changed radically command and events. Please refer to doc and developer tools for change your automations.**
- **Performance boost! (Required re-authentication)**
- Bug fixed on send location.
- Bug fixed on send mulitple buttons.

## 1.1.2

- Bug fixed.
- Performance improvements.

## 1.1.1

- Migration from Home Assistant base image to Debian image
Expand Down
179 changes: 103 additions & 76 deletions whatsapp_addon/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,154 @@

### **How to add other Whatsapp sessions**

Go to configuration page in clients input box digit the desired client_id. This one represents an identifier for the session.

**NOTE:** many sessions equate to more RAM and CPU work. Please do not use more sessions than you need.
Go to configuration page in clients input box digit the desired clientId. This one represents an identifier for the session.

### **How to get a User ID**

The user id is made from three parts:

- Country code (Example 39 (Italy))
- User's number
- And a static part: @c.us
- And a static part: @s.whatsapp.net (for users) @g.us (for groups)

For example for Italian number _3456789010_ the user id is the following _393456789010@c.us_
For example for Italian number _3456789010_ the user id is the following _393456789010@s.whatsapp.net_

### **Send a simple text message**

```yaml
service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: 393456789010@c.us # User ID
clientId: default
to: 391234567890@s.whatsapp.net # User ID
body:
type: text
text: Hello World!
options: # Optional
send_seen: false # If you don't want to send the user that you have read the message (default true)
text: Hi it's a simple text message
```
### **How to send a media**
### **How to send an image**
```yaml
service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: 393456789010@c.us # User ID
clientId: default
to: 391234567890@s.whatsapp.net
body:
type: media_url
url: https://dummyimage.com/600x400/000/fff.png
options: # Optional
caption: Image or video caption
send_seen: false
image:
url: "https://dummyimage.com/600x400/000/fff.png"
caption: Simple text
```
### **How to send audio message**
```yaml
service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: 393456789010@c.us # User ID
clientId: default
to: 391234567890@s.whatsapp.net
body:
type: media_url
url: https://github.com/giuseppecastaldo/ha-addons/blob/main/whatsapp_addon/examples/hello_world.mp3?raw=true
options: # Optional
send_seen: true
send_audio_as_voice: true
audio:
url: "https://github.com/giuseppecastaldo/ha-addons/blob/main/whatsapp_addon/examples/hello_world.mp3?raw=true"
ptt: true # Send audio as a voice
```
### **How to send a location**
```yaml
service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: 393456789010@c.us # User ID
clientId: default
to: 391234567890@s.whatsapp.net
body:
type: location
latitude: 45.0903
longitude: 34.07830
description: New York City
location:
degreesLatitude: 24.121231
degreesLongitude: 55.1121221
```
### **How to send buttons (FIXED)**
### **How to send buttons**
```yaml
service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: 393456789010@c.us # User ID
clientId: default
to: 393456789012@s.whatsapp.net
body:
type: buttons
text: Hi it's a button message
footer: Hello World
buttons:
body: Body
buttons:
- id: 1234 # Optional, random value if empty
body: Button1
- id: 5678 # Optional, random value if empty
body: Button1
title: Title
footer: Footer
- buttonText:
displayText: Button 1
- buttonText:
displayText: Button 2
- buttonText:
displayText: Button 3
- buttonText:
displayText: Button 4
- buttonText:
displayText: Button 5
- buttonText:
displayText: Button 6
```
### **How to send list**
```yaml
service: whatsapp.send_message
data:
clientId: default
to: 391234567890@s.whatsapp.net
body:
text: This is a list
footer: "Footer"
title: Amazing boldfaced list title
buttonText: "Required, text on the button to view the list"
sections:
- title: Section 1
rows:
- title: Option 1
rowId: option1
- title: Option 2
rowId: option2
description: This is a description
- title: Section 2
rows:
- title: Option 3
rowId: option3
- title: Option 4
rowId: option4
description: This is another description
```
### **How to send list (Android devices only)**
```yaml
service: whatsapp.send_message
data:
clientId: default
to: 391234567890@s.whatsapp.net
body:
text: Hi it's a template message
footer: Hello World
templateButtons:
- index: 1
urlButton:
displayText: ⭐ Star Whatsapp addon on GitHub!
url: "https://github.com/giuseppecastaldo/ha-addons/tree/main/whatsapp_addon"
- index: 2
callButton:
displayText: Call me!
phoneNumber: +1 (234) 5678-901
- index: 3
quickReplyButton:
displayText: "This is a reply, just like normal buttons!"
id: id-like-buttons-message
```
---
## Events
| Event type | Description |
| -------------------------------- | ----------------------------------------------------------- |
| whatsapp_message_revoke_everyone | Emitted when a message is deleted for everyone in the chat. |
| new_whatsapp_message | The message that was received |
| Event type | Description |
| -------------------- | ----------------------------- |
| new_whatsapp_message | The message that was received |
---
Expand All @@ -117,14 +165,13 @@ data:
event_type: new_whatsapp_message
condition:
- condition: template
value_template: "{{ trigger.event.data.body == '!ping' }}"
value_template: "{{ trigger.event.data.message.conversation == '!ping' }}"
action:
- service: whatsapp.send_message
data:
client_id: "{{ trigger.event.data.client_id }}" # Which instance of whatsapp should the message come from
to: "{{ trigger.event.data.from }}"
clientId: default
to: "{{ trigger.event.data.key.remoteJid }}"
body:
type: text
text: pong
mode: single
```
Expand All @@ -142,11 +189,10 @@ data:
action:
- service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: 393456789010@c.us
clientId: default
to: 391234567890@s.whatsapp.net
body:
type: text
text: I'm at home
text: Hi, I'm at home
mode: single
```
Expand All @@ -160,28 +206,9 @@ data:
action:
- service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
clientId: default # Which instance of whatsapp should the message come from
to: "{{ trigger.event.data.from }}"
body:
type: text
text: Sorry, I'm driving, I will contact you soon
mode: single
```
```yaml
- alias: Revoked message
description: ""
trigger:
- platform: event
event_type: whatsapp_message_revoke_everyone
condition: []
action:
- service: whatsapp.send_message
data:
client_id: default # Which instance of whatsapp should the message come from
to: "{{ trigger.event.data.before.from }}"
body:
type: text
text: "Revoked message: _{{ trigger.event.data.before.body }}_"
mode: single
```
9 changes: 1 addition & 8 deletions whatsapp_addon/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
ARG BUILD_FROM
FROM $BUILD_FROM

ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

RUN apt install curl \
&& curl -sL https://deb.nodesource.com/setup_18.x | bash - \
&& apt update \
&& apt install nodejs \
&& apt install -y chromium git build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
RUN apk add --no-cache git npm --repository=http://dl-cdn.alpinelinux.org/alpine/v3.10/main

COPY . /
RUN chmod a+x /run.sh
RUN chmod a+x /finish.sh

RUN cd / && npm install
RUN npm install github:pedroslopez/whatsapp-web.js#fix-buttons-list

EXPOSE 3000
CMD [ "/run.sh" ]
2 changes: 0 additions & 2 deletions whatsapp_addon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ _Write your Whatsapp message from Home Assistant_

A WhatsApp API client that connects through the WhatsApp Web browser app

It uses Puppeteer and [whatsapp-web.js](https://wwebjs.dev/) to run a real instance of Whatsapp Web to avoid getting blocked.

**NOTE:** I can't guarantee you will not be blocked by using this method, although it has worked for me. WhatsApp does not allow bots or unofficial clients on their platform, so this shouldn't be considered totally safe.

# Installation guide
Expand Down
82 changes: 82 additions & 0 deletions whatsapp_addon/WhatsappClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const EventEmitter = require('events');

const makeWASocket = require('@adiwajshing/baileys').default
const {
DisconnectReason,
useSingleFileAuthState,
Browsers
} = require('@adiwajshing/baileys')

class WhatsappClient extends EventEmitter {
#sock;

constructor(sessionPath) {
super();
var self = this;
async function connectToWhatsApp() {
const { state, saveState } = useSingleFileAuthState(sessionPath)

self.#sock = makeWASocket({
auth: state,
syncFullHistory: false,
browser: Browsers.macOS('Desktop'),
logger: require("pino")({ level: 'silent' })
})

self.#sock.ev.on('creds.update', saveState)

self.#sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect, qr } = update

if (qr) {
self.emit('qr', qr)
}

if (connection === 'close') {
const shouldReconnect = lastDisconnect.error.output?.statusCode !== DisconnectReason.loggedOut
if (shouldReconnect) {
connectToWhatsApp()
} else {
self.emit('disconnected')
}
} else if (connection === 'open') {
self.emit('ready')
}
})

self.#sock.ev.on('messages.upsert', (Message) => {
let msg = Message.messages[0]
if (!msg.key.fromMe)
self.emit('message', msg)
})
}

connectToWhatsApp()
}

async sendMessage(chatId, msg, options) {
return await this.#sock.sendMessage(chatId, msg, options);
}

async presenceSubscribe(chatId) {
return await this.#sock.presenceSubscribe(chatId);
}

async updateProfileStatus(status) {
return await this.#sock.updateProfileStatus(status)
}

async updateProfileName(name) {
return await this.#sock.updateProfileName(name)
}

async isOnWhatsapp(id) {
return await this.#sock.onWhatsApp(id)
}

async updateBlockStatus(id, status) {
return await this.#sock.updateBlockStatus(id, status)
}
}

module.exports = WhatsappClient
Loading

0 comments on commit b0ba1c7

Please sign in to comment.