Skip to content

Commit

Permalink
BugFix & Removed Feature & Updated Roadmap
Browse files Browse the repository at this point in the history
+ Added QRAuthorize.java
+ Added JFrame QRCode printer for initial run.
+ Removed TaskChain reason of new WhatsApp upcoming update.
+ Added Command Feature to TODO List.
  • Loading branch information
poyrazinan committed Sep 15, 2021
1 parent 2e6582a commit 4ba2241
Show file tree
Hide file tree
Showing 28 changed files with 121 additions and 49 deletions.
Binary file modified Data.xlsx
Binary file not shown.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
- Send a message to multiple contacts etc. (905xxxxxxxx1,905xxxxxxxx2)
- Send message to all of your contacts with **Herkes** boolean. (evet = true, hayır = false)
- And easy data modification with excel.
- Making connection only when try to execute task. With this feature it's not try to have WhatsApp Web all the time and you can use WhatsApp Web without use here function.

## Dependencies

Expand All @@ -29,7 +28,8 @@ All dependencies are managed by Maven Shade Plugin.
* [Github](https://github.com/JetBrains/java-annotations "Github")

## TODO Feature
* ~~Whatsapp auto connect socket when task occur instead of always on.~~
* ~~Taskchain to make task after socket connection~~
* Web authentication QRCode isn't working properly. **Bug Fix**
* ~~Remote task creator with WhatsApp message to self.~~ (Cancelled due TaskChain with Whatsapp disconnecter feature)
* ~~Whatsapp auto connect socket when task occur instead of always on.~~
* ~~Taskchain to make task after socket connection~~
**(Cancelled due WhatsAppWeb new Beta program. Will be update for it when release.)**
* ~~Web authentication QRCode isn't working properly. **Bug Fix**~~
* Remote task creator with WhatsApp message to self. ⌛
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
<scope>scope</scope>
<scope>compile</scope>
</dependency>
<!-- Apache poi-ooxml for excel file reader -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
<scope>scope</scope>
<scope>compile</scope>
</dependency>
<!-- Jetbrains annotations for notnull -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>scope</scope>
<scope>compile</scope>
</dependency>

</dependencies>

<repositories>
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/tr/com/poyrazinan/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package tr.com.poyrazinan;

import it.auties.whatsapp4j.manager.WhatsappDataManager;
import it.auties.whatsapp4j.whatsapp.WhatsappAPI;
import it.auties.whatsapp4j.whatsapp.WhatsappConfiguration;
import lombok.SneakyThrows;
import tr.com.poyrazinan.model.Task;
import tr.com.poyrazinan.services.ExcelInputReader;
import tr.com.poyrazinan.services.MessageSender;
Expand All @@ -13,18 +14,27 @@

public class Main {

public static boolean isConnected = false;

// Imported excel values which is used in timer.
public static List<Task> inputs = new ArrayList<>();

public static void main(String[] args) throws IOException {

new ExcelInputReader();
var api = new WhatsappAPI();

var configuration = WhatsappConfiguration.builder()
.whatsappUrl("wss://web.whatsapp.com/ws")
.requestTag("requestTag")
.description("AutoWPMsg")
.shortDescription("AutoWPMsg")
.reconnectWhenDisconnected((reason) -> false)
.async(true)
.build();

var api = new WhatsappAPI(configuration);
api.registerListener(new WhatsAppListener(api));
new MessageTimer(api);
new MessageSender(api);
MessageTimer.startTimer();
api.connect();
// Do other stuffs on WhatsAppListener#onLoggedIn
// Because if you do here it wont sync to whatsapp connection.
// If you want async then execute here
Expand Down
58 changes: 48 additions & 10 deletions src/main/java/tr/com/poyrazinan/WhatsAppListener.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package tr.com.poyrazinan;

import com.google.zxing.common.BitMatrix;
import it.auties.whatsapp4j.listener.WhatsappListener;

import it.auties.whatsapp4j.response.impl.json.UserInformationResponse;
import it.auties.whatsapp4j.whatsapp.WhatsappAPI;
import lombok.NonNull;
import tr.com.poyrazinan.model.Task;
import lombok.SneakyThrows;
import tr.com.poyrazinan.services.CacheCreator;
import tr.com.poyrazinan.services.MessageSender;
import tr.com.poyrazinan.utils.MessageTimer;
import tr.com.poyrazinan.utils.QRAuthorize;

import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.*;

public class WhatsAppListener implements WhatsappListener {

WhatsappAPI api;
private JFrame QRCodeFrame = new JFrame();

public WhatsAppListener(WhatsappAPI api) {
this.api = api;
Expand All @@ -27,10 +30,13 @@ public WhatsAppListener(WhatsappAPI api) {
*/
@Override
public void onLoggedIn(@NonNull UserInformationResponse response) {
System.out.println("Whatsapp socket connected.");
Main.isConnected = true;
// CacheCreator also execute waiting tasks
if (QRCodeFrame.isEnabled())
QRCodeFrame.dispose();

new CacheCreator(api);
MessageTimer.startTimer();

System.out.println("Whatsapp bağlantısı yapıldı.");
}

/**
Expand All @@ -39,8 +45,40 @@ public void onLoggedIn(@NonNull UserInformationResponse response) {
*/
@Override
public void onDisconnected() {
System.out.println("Whatsapp socket disconnected.");
Main.isConnected = false;
System.out.println("Whatsapp bağlantısı kesildi.");
}

}
/**
* This method for only initial run. It create QRCode on screen.
*
* @param matrix
*/
@SneakyThrows
@Override
public void onQRCode(@NonNull BitMatrix matrix) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

// Removing frame if already opened.
// Execute only when updating QR
if (QRCodeFrame.isEnabled())
QRCodeFrame.dispose();

QRCodeFrame.setUndecorated(true);

// Fetching QRCode and scaling it
ImageIcon image = new ImageIcon(
QRAuthorize.generateQRCodeImage(matrix)
.getScaledInstance(256, 256, java.awt.Image.SCALE_SMOOTH));

JLabel lbl = new JLabel(image);
QRCodeFrame.getContentPane().add(lbl);
// Sets the frame size to match with QRCode
QRCodeFrame.setSize(256, 256);

int x = (screenSize.width - QRCodeFrame.getSize().width)/2;
int y = (screenSize.height - QRCodeFrame.getSize().height)/2;

QRCodeFrame.setLocation(x, y);
QRCodeFrame.setVisible(true);
}
}
3 changes: 1 addition & 2 deletions src/main/java/tr/com/poyrazinan/model/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ public class Task {
public float getUnix() {
return date.getTime() / 1000L;
}

}
}
19 changes: 8 additions & 11 deletions src/main/java/tr/com/poyrazinan/services/CacheCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@
import it.auties.whatsapp4j.protobuf.chat.Chat;
import it.auties.whatsapp4j.whatsapp.WhatsappAPI;
import org.jetbrains.annotations.NotNull;
import tr.com.poyrazinan.model.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class CacheCreator {

// Tasks which isn't executed yet and waiting for connection.
public static List<Task> awaitTasks = new ArrayList<>();

/**
* Creating cache with sending message to owner.
* With this method WhatsAppWeb4J thought it's a message event.
* And caching all the chats to the memory.
*
* @param api
*/
public CacheCreator(@NotNull WhatsappAPI api) {
try {
Chat chat = api.queryChat(api.manager().phoneNumberJid()).get().data();
api.sendMessage(chat, "Sohbet modülleri yükleniyor.");
api.sendMessage(chat, "Bağlantı gerçekleştirildi.");
}
catch (ExecutionException | InterruptedException e) {
System.out.println("Cache verisi oluşturulamadı.");
}

if (!awaitTasks.isEmpty())
new ArrayList<>(awaitTasks).forEach(MessageSender::sendMessage);

}
}
3 changes: 0 additions & 3 deletions src/main/java/tr/com/poyrazinan/services/MessageSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public static void sendMessage(@NotNull Task task) {
.map(Object::toString).collect(Collectors.joining(", "))+
" Mesaj: " + task.getMessage());
}

CacheCreator.awaitTasks.remove(task);
api.disconnect();
}

/**
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/tr/com/poyrazinan/utils/MessageTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.jetbrains.annotations.NotNull;
import tr.com.poyrazinan.Main;
import tr.com.poyrazinan.model.Task;
import tr.com.poyrazinan.services.CacheCreator;
import tr.com.poyrazinan.services.MessageSender;

import java.util.ConcurrentModificationException;
import java.util.Timer;
Expand All @@ -29,7 +29,7 @@ public static void startTimer() {
@Override
public void run() {
try {
Main.inputs.stream().forEach(MessageTimer::checkDate);
Main.inputs.forEach(MessageTimer::checkDate);
}
catch (ConcurrentModificationException e) {
System.out.println
Expand All @@ -50,23 +50,19 @@ public static void stopTimer() {
* @param task
*/
private static void checkDate(@NotNull Task task) {

long unixNow = System.currentTimeMillis() / 1000L;

if (task.isExecuted())
return;

if (task.getUnix() == unixNow) {
task.setExecuted(true);
CacheCreator.awaitTasks.add(task);
api.connect();
return;
MessageSender.sendMessage(task);
}
else if (task.getUnix() < unixNow) {

else if (task.getUnix() < unixNow)
task.setExecuted(true);
return;
}

else return;
return;
}
}
18 changes: 18 additions & 0 deletions src/main/java/tr/com/poyrazinan/utils/QRAuthorize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tr.com.poyrazinan.utils;

import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.awt.*;

public class QRAuthorize {

/**
* Generate QRCode in image format.
*
* @param matrix
*/
public static Image generateQRCodeImage(BitMatrix matrix) {
return MatrixToImageWriter.toBufferedImage(matrix);
}
}
Binary file added target/AutoWPMsg-1.0-SNAPSHOT-shaded.jar
Binary file not shown.
Binary file added target/AutoWPMsg-1.0-SNAPSHOT.jar
Binary file not shown.
Binary file added target/classes/tr/com/poyrazinan/Main.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added target/classes/tr/com/poyrazinan/model/Task.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Wed Sep 15 16:25:11 EEST 2021
groupId=tr.com.poyrazinan
artifactId=AutoWPMsg
version=1.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tr\com\poyrazinan\exceptions\NotInMemoryException.class
tr\com\poyrazinan\Main.class
tr\com\poyrazinan\utils\MessageTimer$1.class
tr\com\poyrazinan\model\Task.class
tr\com\poyrazinan\services\MessageSender.class
tr\com\poyrazinan\services\ExcelInputReader.class
tr\com\poyrazinan\utils\MessageTimer.class
tr\com\poyrazinan\WhatsAppListener.class
tr\com\poyrazinan\services\CacheCreator.class
tr\com\poyrazinan\model\Task$TaskBuilder.class
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\Main.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\services\CacheCreator.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\services\MessageSender.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\utils\MessageTimer.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\utils\QRAuthorize.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\exceptions\NotInMemoryException.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\model\Task.java
C:\Users\emk\IdeaProjects\AutoWPMsg\src\main\java\tr\com\poyrazinan\WhatsAppListener.java
Empty file.
Binary file added target/original-AutoWPMsg-1.0-SNAPSHOT.jar
Binary file not shown.

0 comments on commit 4ba2241

Please sign in to comment.