Facebook Messenger bot using Spring Boot? - java

Nowadays more and more chat bots are being created, as the process is relatively easy if you activate a facebook developer account, and create a facebook page for this purpose.
Probably the most popular method is to using NodeJS - Heroku, as it is explained in the following article:
https://chatbotslife.com/how-to-make-a-facebook-messenger-chat-bot-in-1hr-af6bec5e7aec#.xqgu2lb46
After a research I found that making connection with facebook IS possible via Spring boot (as explained here: https://spring.io/guides/gs/accessing-facebook/ ) but I have not found any information or - even similar to the NodeJS - method to handle a messenger conversation using a Spring Boot server.
Is it possible to request and send facebook messenger messages from Spring Boot? I would prefer that over NodeJS, as I have a system already built in Spring Boot, and it would be much easier to integrate this feature there, than creating a new service, which would be then connected to the existing one. (even if in some cases I definitely would stuck with the 'microservice' structure.)

It is possible.
Cast a glance onto that project:
https://github.com/messenger4j/messenger4j-spring-boot-quickstart-template
Though I understand your reason why you want to 'keep things together', I definitely would recommend to separate things to smaller pieces when you can, as it is a much better strategy generally.

Related

Is there anyway to connect Java to React without having to use Spring?

So I have to build an offline project using MongoDB (with morphia), Java and React/Angular. I'm planning to work with react but all the solutions point to me using Spring Boot or Spring Data for this purpose. Is there no other framework that I could use to make this?
Its a very simple application where we need to list out some database entries and allow a user to make a request and change the entry values based on it.
I know Struts is also an option but remember that Im not actually going to deploy this online. This is like a show and tell to these lecturers on my own device.
NOTE I would have used Spring Data but as far as I know it doesnt have proper support for morphia? I could be wrong. Please let me know if it does
There are literally dozens of Java [micro]frameworks to chose from:
Vert.x
Javalin
Spark
Micronaut
Ratpack
Dropwizard
Ninja
Rapidoid
... and many more.
You can also try Play or Ktor or Grails. Or do some hardcore with Netty or Undertow.
Not to mention plain old servlets...
Vert.x in particular supports reactive MongoDB drivers: https://vertx.io/docs/vertx-mongo-client/java.

Spring boot back end with Swing client

I have a design question: I use DB + Hibernate + SpringBoot as back end in a server and use Swing clients on desktops.
I am considering for a new project the following architecture:
Back end (maybe in separate servers):
SQL DB
Spring Boot + Hibernate Java App
Front end(s):
Swing Java App
later: mobile App; Browser
I have searched a bit, but still have some doubts and feel I am missing something. I initially considered using React for front end. But this is going to take too long for me and I have lots of Swing components that I already optimized (and this my applications tend to use lots of grids with edits "in place")
I searched for Spring + Swing, but what I found was about running everything on the same JVM. I would think that separating them would allow me to (later) build a Mobile or Web front end using the same back end.
To sum it up:
create back end services with spring boot + hibernate on top of SQL db
create Swing front end that consumes those services
(later stage) mobile app to consume these same services
Questions:
Is this a good approach ? Am I seeing this the wrong way
How to "share" the POJOs between the back end and the front end? (thinking about placing the models on a separate lib); I don't mean how to transport them, I mean how to reuse them on both projects.
This is possible.
I would create the backend application with Spring Boot and access a datastore with Spring DATA. I would make services available via REST.
To access REST from your Swing client you must use a REST client of some sort. I would recommend Spring RestTemplate but there are many alternatives.
Synchronous client to perform HTTP requests, exposing a simple,
template method API over underlying HTTP client libraries such as the
JDK HttpURLConnection, Apache HttpComponents, and others.
Since you want mobile support some time in the future you could consider creating a webclient instead and making it cross platform (browser only).
To answer your two questions:
Regarding backend you will be perfectly fine as Spring Boot is a good option. As for the client I personally would not choose Swing today since it is very old and rather go for JavaFX 2 or a web client if that is an option. If you are the only developer and since you are proficient it might be a good solution for you.
You can share the POJOs from you Server for your Client as an api that you include as dependency. However it is usually not recommended to share POJOs but rather generate it on the client side or just type them. In your case if it is a small project it probably is ok. Do what is most easy for you.
I would recommend start using Maven as a build tool if you do not already.
Good luck.
Your request is perfectly valid, as you are a Swing developer and your approach is absolutely fine.
So let's draw the general picture: you're building a backend/frontend product/software.
Backend
Your backend will be a Spring application that will run on a server, let's say http://localhost:8080, serving requests to clients.
You'd probably want to create a #RestController on http://localhost:8080/api/... to expose your functions to your Swing client.
Swing client
Your client will be a Swing application running on your desktop.
To connect your client to the Spring application, you now need to implement a service in your Swing application that will call your Spring web server and fetch the resources from there.
To achieve this goal, may options exist:
Why not plain Java ? Use this tutorial to call your Spring services.
Http components from Apache are quite common.
Spring your app ! Spring API includes RestTemplate that you can use in your Swing app to call Spring backend and fetch resources for your desktop app.
As you can see, this is EXACTLY the same thing as with a website in React:
your web page is the JFrame
your JSX components in React would be your JPanels and components
you'd connect action listeners on components (JComboBox, JButton) to a service calling your Spring backend.
you'd then use the resources fetched from your Spring server to update your application state.
rince and repeat
So, be confident, your approach is correct. You'll find people ranting at you having chosen an obsolete techno but really, who cares?
(May I add, totally a pun, you've been smarter than the JavaFX guys which are now facing the segregation of javafx libraries from the core Java ;-))
And as always... happy coding !

REST API with websocket using Spring boot

I am actually working to my university junior project.
I want to make a game using spring boot, this game will need a WebSocket.
I will surely need to make a REST API for user registration, deleting, updating, posting and getting data.
Am I going to be able to use WebSocket along with REST?
If yes please send me resources that can help me, otherwise please tell me what should I do.
I'm sorry if my question was not so smart but I am new to all of this.
Yes, your Spring application can easily use both WebSocket and REST APIs. If you're going to use Spring Boot, I'd suggest to take a look into their example projects (note the spring-boot-sample-websocket- directories). I think it's the best way to learn.
More on WebSockets, I'd suggest to use STOMP protocol. There is also a nice guide for it on how you can get started.
For your RESTful API, you'll be making simple controllers / services, there is tones of resources for this. You can also follow this guide to get started as well.
While Spring applications support both Websocket and REST API's individually, you can use Websocket connection to send requests to your rest API's too. Needs a little glitching tho, but I've made it work in this github repo. Using this library you can represent your rest-api's in a websocket based protocol.
I also made java (and springboot starter) for client.

Spring Rest API Confusion

I am exploring the possibility of using a RESTful API as the backend for an Android application.
I previously have simply been using socket programming to have my clients connect to server application but since I intend to develop a web application (with the admin functionalities for the android app) I figured this may be a good option.
My backend will be required to pull data from a nosql database and run a machine learning algorithm periodically on the data. The android app will, amongst other things, query the results of the computations and provide additional information to the algorithm.
Is it possible for me to use Spring to expose some of the application functionality through a REST API while still having other functions and tasks running in the background? Basically, can I design an application that will query multiple web services and perform various scheduled computations and query and expose only some of those functions through an API ?
Am I approaching this completely the wrong way?
Your approach sounds fine to me. Your REST API could just call internal backend methods as needed and return their output as JSON or XML formatted data.
I recommend you, if you haven't already looked at them, to go through the Spring "Getting Started" guides for building a RESTful web service and scheduling tasks.
On the Android side I'd recommend you to look into Retrofit.

Push Notification in Spring MVC WebApp

I am currently writing a forum web-application using Spring MVC. I'm just a beginner at Spring and have only been working with it for about 1 week now.
I need to implement push notifications. Here is the scenario: User A logs in and creates a post. User B comments on User A's post while User A is still logged in. User A receives a notification that some user has commented on his post, without his browser refreshing the page.
I need help with sending the notification to User A that User B has commented on his post asynchronously. I have done some research, and found that there is a package called CometD that I can use, but I cannot find any simple tutorials for me to understand.
Can anyone suggest any other packages/way to solve my problem? Or if you have any simple CometD tutorials, that would be great as well.
I am the CometD project lead.
CometD ships with a demo war file that has a built-in chat application that you can use as a basis to understand how CometD works, see https://docs.cometd.org/current/reference/#_installation.
The CometD full reference documentation is here: https://docs.cometd.org.
From experience, I don't recommend using Servlet 3.0 async features to implement a chat, because that will rule out usage of WebSocket.
It's better you rely on frameworks such as CometD that can start with WebSocket (as it is more efficient) and then fallback automatically and transparently to HTTP if WebSocket does not work.
On a similar note, I don't recommend a WebSocket only approach because WebSocket does not work in certain scenarios (for example, mobile networks often do not allow WebSocket traffic), and it's a too low-level protocol so you will have to build your own protocol on top of WebSocket (which is a lot of work and not easy to get right).
CometD provides you with an easy to extend protocol and automatic transport fallback, and has a ton of features (e.g. authorization & authentication and clustering just to mention two).
Have a look at the spring MVC chat sample app for spring MVC 3.2, there is also a spring blog post about it.

Categories