# whatsappy
[![Downloads](https://pepy.tech/badge/whatsappy-py)](https://pepy.tech/project/whatsappy-py)

Whatsappy is a Python library for creating whatsapp bots.

Creator: [Italo Seara](https://github.com/italoseara)

## Installation


Use the package manager [pip](https://pip.pypa.io/en/stable/) to install whatsappy.

```bash
pip install whatsappy-py
```

## Usage

### Basic example:
```python
from whatsappy import Whatsapp

whatsapp = Whatsapp()

whatsapp.login(visible=False) # Login whatsapp (Headless mode)

chat = whatsapp.chat('Mom') # Goes to the selected chat
chat.send('Hello') # Send a message

whatsapp.close() # Exit
```

## 1. Setup

```python
from whatsappy import Whatsapp

whatsapp = Whatsapp() # Initialize the library
```

## 2. Login

### Input
```python
whatsapp.login(visible=False, timeout=0)
```

| Arguments | Type | Default |
| --------- | ---- | ------- |
| visible   | bool | False   |
| timeout   | int  | 0       |

P.S: `timeout` should be in seconds

### Output
![QRCode](https://i.imgur.com/aQjoNdZ.png?1)

### After you scan it, you should wait for a message like this:
```
Successfully logged in
```

## 3. Close

```python
whatsapp.close() # Closes the webdriver
```

## 4.1 Chat

### Input
```python
whatsapp.chat(name="Mom") # Opens the conversation
```

| Arguments | Type | Default |
| --------- | ---- | ------- |
| name      | str  | -       |

### Output

### *Group*

* Properties

  | Property        | Type      | Default |
  | --------------- | --------- | ------- |
  | name            | str       | ""      |
  | description     | str       | ""      |
  | profile_picture | str       | None    |
  | invite_link     | str       | None    |
  | admin           | bool      | False   |
  | participants    | List[str] | []      |
  | last_message    | Any       | -       |

  P.S.: `last_message` will be explained later on

* Functions

  * `send` -> `None`: Sends a message to the chat
    
    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | message   | str  | ""      |
    | file      | str  | ""      |

    P.S.: `file` shoud be a file path

    ```python
    group.send(message="Hi <@Mom>!", file="flowers.jpg") # Use <@Contact Name> to mention someone
    ```
  
  * `add` -> `None`: Adds new participants to the group
  
    | Arguments | Type      | Default |
    | --------- | --------- | ------- |
    | contacts  | List[str] | -       |

    ```python
    group.add(["Grandma", "Cousin"])
    ```

  * `remove` -> `None`: Removes participants from the group
  
    | Arguments | Type      | Default |
    | --------- | --------- | ------- |
    | contacts  | List[str] | -       |

    ```python
    group.remove(["Boring Cousin"])
    ```

  * `promote` -> `None`: Promotes participants to admin

    | Arguments | Type      | Default |
    | --------- | --------- | ------- |
    | contacts  | List[str] | -       |

    ```python
    group.promote(["Dad", "Mom"])
    ```
  
  * `demote` -> `None`: Demotes participants to member

    | Arguments | Type      | Default |
    | --------- | --------- | ------- |
    | contacts  | List[str] | -       |

    ```python
    group.demote(["Brother"])
    ```
  
  * `leave` -> `None`: Leaves the group

    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | -         | -    | -       |

    ```python
    group.leave()
    ```

### *Contact*

* Properties
  
  | Property        | Type | Default |
  | --------------- | ---- | ------- |
  | name            | str  | ""      |
  | number          | str  | ""      |
  | about           | str  | ""      |
  | profile_picture | str  | None    |
  | last_message    | Any  | -       |

  P.S.: `last_message` will be explained later on

* Functions

  * `send` -> `None`: Sends a message to the chat
    
    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | message   | str  | ""      |
    | file      | str  | ""      |

    P.S.: `file` shoud be a file path

    ```python
    group.send(message="Hi <@Mom>!", file="flowers.jpg") # Use <@Contact Name> to mention someone
    ```

## 4.2 Message

The `last_message` property of a chat will return the type of the message (Location, Text, Contact card, etc.) as a class, it can be:

### *Text*

* Properties

  | Property     | Type     | Default |
  | ------------ | -------- | ------- |
  | text         | str      | ""      |
  | chat         | Any      | None    |
  | author       | str      | None    |
  | time         | datetime | None    |
  | forwarded    | bool     | False   |
  | quote        | Quote    | -       |
  | Quote.text   | str      | -       |
  | Quote.author | str      | -       |

* Functions

  * `forward` -> `None`: Forward the message to a list of contacts

    | Arguments | Type      | Default |
    | --------- | --------- | ------- |
    | contacts  | List[str] | -       |

    ```python
    message.forward(["Mom", "Grandma"])
    ```
  
  * `reply` -> `None`: Reply the message quoting the original one

    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | message   | str  | ""      |
    | file      | str  | ""      |

    P.S.: `file` should be a file path

    ```python
    message.reply("Wow, that's awesome!")
    ```

  * `reply_privately` -> `None`: Reply the message in private chat (Only works on groups)

    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | message   | str  | ""      |
    | file      | str  | ""      |

    P.S.: `file` should be a file path
    
    ```python
    message.reply_privately("Long time since i saw you, huh?")
    ```

  * `delete` -> `None`: Deletes the message only for you

    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | -         | -    | -       |

    ```python
    message.delete()
    ```

  * `star` -> `None`: Stars the message for you

    | Arguments | Type | Default |
    | --------- | ---- | ------- |
    | -         | -    | -       |

    ```python
    message.star()
    ```

### *Document*

Inherits all the `Text` properties and functions

* Properties

  | Arguments     | Type  | Default |
  | ------------- | ----- | ------- |
  | file          | File  | None    |
  | File.size     | int   | 0       |
  | File.name     | str   | ""      |
  | File.mimetype | str   | ""      |
  | File.content  | bytes | b""     |

### *Video*

Inherits all the `Text` properties and functions

* Properties

  | Arguments | Type | Default |
  | --------- | ---- | ------- |
  | length    | int  | 0       |

### *Audio*

Inherits all the `Text` properties and functions

* Properties

  | Arguments    | Type  | Default |
  | ------------ | ----- | ------- |
  | file         | File  | None    |
  | File.size    | int   | 0       |
  | File.length  | int   | 0       |
  | File.content | bytes | b""     |
  | isrecorded   | bool  | False   |

### *ContactCard*

Inherits all the `Text` properties and functions

* Properties

  | Arguments       | Type          | Default |
  | --------------- | ------------- | ------- |
  | contacts        | List[Contact] | []      |
  | Contact.name    | str           | ""      |
  | Contact.numbers | List[str]     | []      |

### *Location*

Inherits all the `Text` properties and functions

* Properties

  | Arguments | Type  | Default |
  | --------- | ----- | ------- |
  | coords    | tuple | ()      |
  | link      | str   | ""      |

### *LiveLocation*

Inherits all the `Location` properties and functions

* Properties

  | Arguments | Type     | Default |
  | --------- | -------- | ------- |
  | until     | datetime | None    |

### *Image*

Inherits all the `Text` properties and functions

* Properties

  | Arguments       | Type  | Default |
  | --------------- | ----- | ------- |
  | file            | File  | None    |
  | File.size       | int   | 0       |
  | File.resolution | tuple | ()      |
  | File.content    | bytes | b""     |

### *Sticker*

Inherits all the `Image` properties and functions

* Properties

  | Arguments | Type | Default |
  | --------- | ---- | ------- |
  | -         | -    | -       |

## Create a new Group

### Input
```python
group = whatsapp.new_group(
    name="Family Group", 
    contacts=["Mom", "Dad", "Grandma", "+17873375275"]
)

print(group)
```

### Output
```
Whatsapp.Group(
    name="Family Group", 
    description="", 
    profile_picture=None, 
    invite_link="https://chat.whatsapp.com/J6SwpsK5jzq5h0e5Tx3gBu",
    admin=True
)
```

## Change group information

### Input
```python
group = whatsapp.chat("Family Group")

group.name = "My Family" # Changes group name
group.description = "This is a family group" # Changes group description
group.profile_picture = "Family.jpeg" # Changes group profile picture

print(group)
```

### Output
```
Whatsapp.Group(
    name="My Family", 
    description="This is a family group", 
    profile_picture="https://pps.whatsapp.net/v/t61.24694-24/187559880_805785283641101_6215437820834613111_n.jpg?ccb=11-4&oh=01_AVxyUwoLnQVdZ1bxDOx2zhYMWKKRitosP3wH-tILL8JNMw&oe=61F7B127", 
    invite_link="https://chat.whatsapp.com/J6SwpsK5jzq5h0e5Tx3gBu",
    admin=True
)
```

## Get all your contacts

### Input

```python
contacts = whatsapp.contact_list 
print(contacts)
```

### Output
```
["Mom", "Dad", "Cousin", "Friend", ...]
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License

[MIT](https://github.com/italoseara/whatsappy/blob/main/LICENSE)
