A repository containing the following bots developed in Scala.
Author: https://t.me/Benkio
Each bot contains a resources
folder. You need to put in there
the .token
file containing the telegram key specific to each
bot.
Each bot also uses a SQLite
Database! So you need
to provide the path to the database into the application.conf
or just set up the specific environment variables. No need to put
the files in resources since those will be fetched from the web
via URLs.
Check this video for a complete rundown on how to add files to a bot.
There’s a module called botDB
, if opportunely configured,
when launched, it applies the migrations and populates a bot
database. Just set up the application.conf
correctly with the DB
path, see the DB in the root of the project, and the location of
the json
. There should be a json
file at the root of each bot
module
Several command alias are defined in the project to group together useful sbt
commands:
dbSetup
- Run the botDB main that set up the DB anew. Running the migrations and updating the media tables based on the JSON in the bots folders.
fix
- Run the
scalafmt
andscalafix
in the whole project. check
- Check the project for formatting and dependencies using
sbt
plugins such asscalafmt
. validate
- compile clean and test. It includes the
fix
command and it is run in the CI.
Moreover, there are tests excluded from the CI because they are quite slow. In this category, there is one that checks all the media links in the DB and can be called manually by sbt integration/runIntegrationScalaTests
Under Windows use the git bash terminal.
./lunch.sh # I know it's lunch not launch :)
- Export the webhook host, locally using ngrok, expose or tunnelTo. example:
./expose share http://localhost:8080
- Change the entry
WEBHOOK_HOST_URL
with the name of the host from the previous step or change it in ~~sBots/main/src/main/resources/application.conf~ - run
sbt assembly
- run
java -cp main/target/scala-3.3.1/main.jar com.benkio.main.MainWebhook
TODO: There’s already a main/Dockerfile
with the command to run the webhook, but still it needs to be tested and visible from outside
Also check expose on docker. In order to may make it discoverable from telegram API.
There should be a pipeline on master, running after the other workflows, that should deploy build, deploy and run the bots remotely on Oracle already. Every new master commit will result in a deployment.
- Create a VM instance in the Oracle cloud
- Get the SSH Private and Public Keys to access it
- Login to ssh VM instance by ssh private key, user, and IP. get the last two on the site. Eg command:
ssh -i <<private key of the vm>> <<user of the vm>>@<<public IP of the vm>>
(ssh -i ssh-key-2022-04-26.key [email protected]
)
- Login to ssh VM instance by ssh private key, user, and IP. get the last two on the site. Eg command:
- Install the jdk 16. Follow this tutorial
If something doesn’t work, please follow this guide and update the documentation accondingly Follow these steps to set up the server property:
- Server Prerequisites
- Check this page for the prerequisites required by telegram to work with webhook. In our case, we choose port 8443 for SSL connection.
- Open Port on OCI Subnet
- The instance where the bots are running needs to have an attached VNICs(Virtual Network Interface Card) with a subnet. The subnet will have a default security list containing the rules for the ports available from outside. Add a new rule for the port required and save it. The parameters to use are:
- Stateless: No
- Source: 0.0.0.0
- IP Protocol: TCP
- Source Port Range: All
- Destination Port Range: 8443
This should allow telegram to reach your server through that port.
- Test the port is open
- by spawning a server with the command
python3 -m http.server 8443
and try to open a browser tab to the server. eg http://129.152.27.196:8443/ - Generate a keystore JKS
- On the server, using the commands on this page, follow the instruction for the java keystore, pasting each command one by one. For the correct keystore password to use, check the `application.conf`. Example
$ keytool -genkey -alias sbots -keyalg RSA -keystore sbotsKeystore.jks -keysize 2048 -validity 360 Enter keystore password: Re-enter new password: Enter the distinguished name. Provide a single dot (.) to leave a sub-component empty or press ENTER to use the default value in braces. What is your first and last name? [Unknown]: 129.152.27.196 What is the name of your organizational unit? [Unknown]: What is the name of your organization? [Unknown]: What is the name of your City or Locality? [Unknown]: What is the name of your State or Province? [Unknown]: What is the two-letter country code for this unit? [Unknown]: Is CN=129.152.27.196, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? [no]: yes Generating 2,048 bit RSA key pair and self-signed certificate (SHA384withRSA) with a validity of 360 days for: CN=129.152.27.196, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown
- Convert the keystore to PEM
- following the commands in the link above. eg:
$ keytool -importkeystore -srckeystore sbotsKeystore.jks -destkeystore sbots.p12 -srcstoretype jks -deststoretype pkcs12 Importing keystore sbotsKeystore.jks to sbots.p12... Enter destination keystore password: Re-enter new password: Enter source keystore password: Entry for alias sbots successfully imported. Import command completed: 1 entries successfully imported, 0 entries failed or cancelled $ openssl pkcs12 -in sbots.p12 -out sbotsCertificatePub.pem -nokeys
- Set the
main/src/resources/application.conf
accordingly:webhook-base-url
ashttps://<serverip>:<outsideport>
, eghttps://129.152.27.196:8443
host-url
as0.0.0.0
port
as<internalPort>
in our case8443
webhook-certificate
with the path of the public certificate, egsbotsCertificatePub.pem
keystore-path
add the path to the keystore, egsbotsKeystore.jks
keystore-password
add the password of the keystore, egsbotsKeystorePassword
- Run
sbt "clean; main/assembly"
to create the fat jar - Move the fat jar to the VM by
rsync
andssh
. Eg.rsync -P -e "ssh -i <<private key of the vm>>" <<path to the fat jar -> sBots/main/target/scala-2.13/main.jar>> <<user of the vm>>@<<public IP of the vm>>:/home/<<user of the vm>>/main.jar
(rsync -P -e "ssh -i ubuntu_rsa.pem" /home/benkio/workspace/sBots/main/target/scala-2.13/main.jar [email protected]:/home/ubuntu/bots/main.jar
) - Move the
botDB.sqlite
if not present in the same way before. If an update to themedia
needs to be done, better to dump the current database in order not to lose thetimeout
,subscription
, or other changes in the process. No easy way to migrate the database as of now. - Login to the VM
- OPTIONAL: be sure to have the right environment variables. IT’S RECOMMENDED TO
CHANGE the
application.conf
(point 1) before running theassembly
. The environment variables could lose their value somehow. - Run the bots.
- Polling:
java -Xmx512m -Xms512m -cp main.jar com.benkio.main.MainPolling
- Webhook:
java -Xmx512m -Xms512m -cp main.jar com.benkio.main.MainWebhook
- Polling:
- press
Ctrl+Z
, runbg
anddisown
in order to let previous command run in background - close your terminal and enjoy