Hello and welcome to yet another Camel development series. In this post I will describe how to use some basic Camel concepts together with Telegram chat bot and accessing a REST API to receive chat messages. You can customize this for many use causes for push/pull type of apps. Finally I will show how you can deploy this non-web app to Heroku and see the result.
The basic flow of the app is as follows:
- On startup it calls a REST API found at svenskaspel.se which is the swedish government’s lottery site. They have a REST API where you can access various lotteries, draws and results.
- The app first calls an endpoint to get the draws for this week.
- It then retrieves the draw number for Saturday and Wednesday draw.
- It calls another endpoint and receives the actual lottery numbers for the draws for those days.
- It then pushes out a friendly message to a Telegram chat bot.
Now off course, you can add additional features such as sending commands and getting more data and even returning random numbers to suggest for playing the lottery.
First the code
You can find all the code here https://github.com/SoucianceEqdamRashti/svenskaspel . I will not got through code in detail such most Camel users should be familiar with it since it is pretty standard Camel functionality.
To get our app to deploy to Heroku we need to be able to run our pom file with the package command. To to do that we need to assemble our app and this can be done using the appassembler-maven-plugin. Simply add the plugin below and change the parts to your project. Pay attention to the CamelWorker which is specified under target. This will be used by Heroku when starting our worker process.
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>appassembler-maven-plugin</artifactId> <version>1.1.1</version> <configuration> <assembleDirectory>target</assembleDirectory> <programs> <program> <mainClass>org.souciance.camel.lotto.MainApp</mainClass> <name>CamelWorker</name> </program> <program> <mainClass>org.souciance.camel.lotto.MainApp</mainClass> <name>svenskalotto</name> </program> </programs> </configuration> <executions> <execution> <phase>package</phase><goals><goal>assemble</goal></goals> </execution> </executions> </plugin>
Now we come to the Heroku setup. Let’s follow these setups.
1. Create an account at Heroku.
2. Download the Heroku cli and install it.
3. Before proceeding ensure that your folder structure is allows:
That is, don’t have a long folder structure leading to your pom file. This is because Heroku looks at the root folder for the pom.xml, system.properties and the Procfile. If your repo is not structured like this rearrange it before proceeding.
4. Verify by opening a cmd and type ”heroku” in the command prompt. You should receive a prompt to login. Enter your credentials to login. Afterwards next time just typing heroku should display the following:
5. Then it is time to create your heroku app. In the command prompt heroku apps:create :
The link above should the remote git repo that you need to add to your remote link in your local git repository. I called mine heroku. Ensure that if you type git remote -v, the ”heroku” remote is shown with the link to your app repo.
5. Now in order for our app to work in Heroku a couple things are needed. Heroku by default works with webapps where you have some website to show. Our Camel app is java standalone app. We instead need to use a Heroku worker process rather than a webapp process. This is so that Heroku understands that our app is a ”backend” app. For more on this look into Heroku processes. We also need to specify the jdk to use, add jvm arguments to ignore ssl certification validations and create an environment variable to store our API access key.
6. Heroku requires two files, a system.properties file and a Procfile. Create both of them and put them in the root folder of your repo. You can look at my repo https://github.com/SoucianceEqdamRashti/svenskaspel to see what values I used. Essentially the system.properties file tells Heroku how to run our app and parameters to use when running it. Procfile contains the type of worker to use on the Dyno.
7. Once you have added the two files remember to push to your repo.
8. Now it is time to push to your heroku repo. In your cmd at your project folder type:
git push heroku master
You should now see heroku push your code and building the app as can been here:
Finally the build process finishes as seen here:
If the app did not start immediately you can view the logs using:
9. Now, our app will start but will crash. Why? Because we have not created an environment variable four access key. Login to our app dashboard. Go to settings. There is a option to show configVars. Click and then a key value input form will appear. Write the name of your environment variable and its value. Ensure you have referred to it in your Camel code. Once you save it the app will restart.
10. If everything works your Telegram bot should show some nice lotto text 😉 For example:
If you have any questions on the code or the setup let me know. Eventually I hope to expand and do a more push based chat.