Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
whatsappweb
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mauro Paolo Josue Zuñiga Mallqui
whatsappweb
Commits
f458b68d
Commit
f458b68d
authored
Jul 16, 2024
by
Mauro Paolo Josue Zuñiga Mallqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MEJORAS
parent
1ca0d994
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
45 deletions
+35
-45
WhatsappRest.java
src/main/java/sacooliveros/whatsappweb/api/WhatsappRest.java
+23
-39
WhatsappBean.java
...main/java/sacooliveros/whatsappweb/bean/WhatsappBean.java
+6
-1
WhatsappUtils.java
...in/java/sacooliveros/whatsappweb/utils/WhatsappUtils.java
+6
-5
No files found.
src/main/java/sacooliveros/whatsappweb/api/WhatsappRest.java
View file @
f458b68d
package
sacooliveros
.
whatsappweb
.
api
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.TimeoutException
;
import
java.util.logging.Logger
;
import
org.apache.catalina.connector.Response
;
import
org.springframework.http.MediaType
;
...
...
@@ -26,54 +23,43 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils;
public
class
WhatsappRest
{
Logger
logger
=
Logger
.
getLogger
(
WhatsappRest
.
class
.
getName
());
Whatsapp
Bean
whatsappBean
;
Whatsapp
whatsapp
;
WhatsappService
whatsappService
;
public
WhatsappRest
(
WhatsappBean
whatsapp
Bean
,
WhatsappService
whatsappService
)
{
this
.
whatsapp
Bean
=
whatsappBean
;
WhatsappUtils
utils
;
public
WhatsappRest
(
WhatsappBean
whatsapp
,
WhatsappService
whatsappService
)
{
this
.
whatsapp
=
whatsapp
.
getWhatsapp
()
;
this
.
whatsappService
=
whatsappService
;
}
@GetMapping
(
"qr"
)
public
ResponseEntity
<?>
getQR
()
throws
ExecutionException
,
InterruptedException
,
TimeoutException
{
Whatsapp
w
=
whatsappBean
.
getWhatsapp
();
WhatsappUtils
utils
=
new
WhatsappUtils
();
if
(
w
==
null
)
{
public
ResponseEntity
<?>
getQR
()
{
if
(
whatsapp
==
null
)
{
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
utils
.
generateQR
());
}
w
.
connect
().
get
(
5
,
TimeUnit
.
SECONDS
);
if
(
w
.
isConnected
())
{
whatsapp
.
connect
().
join
();
if
(
whatsapp
.
isConnected
())
{
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
"Already connected"
);
}
try
{
w
.
connect
().
get
(
5
,
TimeUnit
.
SECONDS
);
}
catch
(
InterruptedException
|
ExecutionException
|
TimeoutException
e
)
{
logger
.
severe
(
e
.
getMessage
());
return
ResponseEntity
.
status
(
Response
.
SC_INTERNAL_SERVER_ERROR
).
body
(
"Connection failed"
);
}
if
(!
w
.
isConnected
())
{
return
ResponseEntity
.
status
(
Response
.
SC_INTERNAL_SERVER_ERROR
).
body
(
"Connection failed"
);
}
else
{
return
ResponseEntity
.
status
(
Response
.
SC_
INTERNAL_SERVER_ERROR
).
body
(
"Try logging out first"
);
return
ResponseEntity
.
status
(
Response
.
SC_
OK
).
body
(
utils
.
generateQR
()
);
}
}
@GetMapping
(
"logout"
)
public
ResponseEntity
<?>
logout
()
{
Whatsapp
w
=
whatsappBean
.
getWhatsapp
();
if
(
w
==
null
)
{
if
(
whatsapp
==
null
)
{
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
"Not connected"
);
}
System
.
out
.
println
(
whatsapp
.
store
().
about
());
try
{
w
.
disconnect
().
get
(
5
,
TimeUnit
.
SECONDS
);
w
.
logout
().
get
(
5
,
TimeUnit
.
SECONDS
);
if
(
whatsapp
.
isConnected
()){
whatsapp
.
disconnect
().
join
();
whatsapp
.
logout
().
join
();
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
"Logged out"
);
}
catch
(
InterruptedException
|
ExecutionException
|
TimeoutException
e
)
{
}
else
{
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
"Not connected"
);
}
}
catch
(
Exception
e
)
{
logger
.
severe
(
e
.
getMessage
());
return
ResponseEntity
.
status
(
Response
.
SC_INTERNAL_SERVER_ERROR
).
body
(
"Logout failed"
);
}
...
...
@@ -81,15 +67,13 @@ public class WhatsappRest {
@PostMapping
(
value
=
"send"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
public
ResponseEntity
<?>
send
(
@RequestParam
Map
<
String
,
String
>
payload
,
@RequestParam
Map
<
String
,
MultipartFile
>
files
)
{
Whatsapp
w
=
whatsappBean
.
getWhatsapp
();
if
(
w
==
null
)
{
if
(
whatsapp
==
null
)
{
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
"Not connected"
);
}
try
{
w
.
reconnect
().
get
(
5
,
TimeUnit
.
SECONDS
);
System
.
out
.
println
(
w
.
store
().
about
());
if
(
w
.
isConnected
())
{
whatsappService
.
send
(
payload
,
files
,
w
);
whatsapp
.
connect
().
join
();
if
(
whatsapp
.
isConnected
())
{
whatsappService
.
send
(
payload
,
files
,
whatsapp
);
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
payload
);
}
else
{
return
ResponseEntity
.
status
(
Response
.
SC_OK
).
body
(
"Not connected"
);
...
...
src/main/java/sacooliveros/whatsappweb/bean/WhatsappBean.java
View file @
f458b68d
...
...
@@ -9,10 +9,15 @@ import it.auties.whatsapp.api.Whatsapp;
@Component
public
class
WhatsappBean
{
Optional
<
Whatsapp
>
w
=
Whatsapp
.
webBuilder
().
la
stConnection
().
registered
();
Optional
<
Whatsapp
>
w
=
Whatsapp
.
webBuilder
().
fir
stConnection
().
registered
();
public
Whatsapp
getWhatsapp
()
{
if
(
w
.
isEmpty
()){
w
=
Whatsapp
.
webBuilder
().
lastConnection
().
registered
();
return
w
.
orElse
(
null
);
}
else
{
return
w
.
get
();
}
}
}
src/main/java/sacooliveros/whatsappweb/utils/WhatsappUtils.java
View file @
f458b68d
...
...
@@ -13,8 +13,13 @@ import it.auties.whatsapp.api.Whatsapp;
public
class
WhatsappUtils
{
Whatsapp
whatsapp
;
Logger
logger
=
Logger
.
getLogger
(
WhatsappUtils
.
class
.
getName
());
public
WhatsappUtils
(
Whatsapp
whatsapp
)
{
this
.
whatsapp
=
whatsapp
;
}
public
String
generateQR
()
{
StringBuilder
qr
=
new
StringBuilder
();
CountDownLatch
latch
=
new
CountDownLatch
(
1
);
...
...
@@ -26,11 +31,7 @@ public class WhatsappUtils {
try
{
CompletableFuture
.
supplyAsync
(()
->
Whatsapp
.
webBuilder
()
.
lastConnection
()
.
unregistered
(
QrHandler
.
toString
(
smallQrConsumer
))
.
connect
()
.
join
()
Whatsapp
.
webBuilder
().
newConnection
().
unregistered
(
QrHandler
.
toString
(
smallQrConsumer
)).
connect
().
join
()
).
get
(
10
,
TimeUnit
.
SECONDS
);
if
(!
latch
.
await
(
10
,
TimeUnit
.
SECONDS
))
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment