/sBots

Collection of Scala bots based on different characters

Primary LanguageScala

sBots

https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fbenkio%2FsBots%2Fbadge&style=flat https://img.shields.io/badge/Scala_Steward-helping-blue.svg?style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAMAAAARSr4IAAAAVFBMVEUAAACHjojlOy5NWlrKzcYRKjGFjIbp293YycuLa3pYY2LSqql4f3pCUFTgSjNodYRmcXUsPD/NTTbjRS+2jomhgnzNc223cGvZS0HaSD0XLjbaSjElhIr+AAAAAXRSTlMAQObYZgAAAHlJREFUCNdNyosOwyAIhWHAQS1Vt7a77/3fcxxdmv0xwmckutAR1nkm4ggbyEcg/wWmlGLDAA3oL50xi6fk5ffZ3E2E3QfZDCcCN2YtbEWZt+Drc6u6rlqv7Uk0LdKqqr5rk2UCRXOk0vmQKGfc94nOJyQjouF9H/wCc9gECEYfONoAAAAASUVORK5CYII= https://typelevel.org/cats/img/cats-badge-tiny.png

A repository containing the following bots developed in Scala.

Author: https://t.me/Benkio

Bots

Telegram BotCharacter Link
https://t.me/RichardPHJBensonBotRichard Philip Henry John Benson
https://t.me/YouTuboAncheI0BotYoutubo Anche Io
https://t.me/ABarberoBotAlessandro Barbero
https://t.me/M0sconiBotGermano Mosconi
https://t.me/XahLeeBotXah Lee
https://t.me/CalandroBot

Required Software

Add Data to the Bots

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.

Database Setup

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

Compile and Test

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 and scalafix in the whole project.
check
Check the project for formatting and dependencies using sbt plugins such as scalafmt.
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

Run the Bots

Long Polling

Under Windows use the git bash terminal.

./lunch.sh # I know it's lunch not launch :)

Webhook

Localhost

  • 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

Docker

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.

Continuous Deployment

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.

Deploy on Oracle Cloud Infrastructure VM

Setup

  • 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 opc@140.238.155.16)
  • Install the jdk 16. Follow this tutorial

Webhook Extra Configuration

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
    

Deploy

  • Set the main/src/resources/application.conf accordingly:
    • webhook-base-url as https://<serverip>:<outsideport>, eg https://129.152.27.196:8443
    • host-url as 0.0.0.0
    • port as <internalPort> in our case 8443
    • webhook-certificate with the path of the public certificate, eg sbotsCertificatePub.pem
    • keystore-path add the path to the keystore, eg sbotsKeystore.jks
    • keystore-password add the password of the keystore, eg sbotsKeystorePassword
  • Run sbt "clean; main/assembly" to create the fat jar
  • Move the fat jar to the VM by rsync and ssh. 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 ubuntu@129.152.27.196:/home/ubuntu/bots/main.jar)
  • Move the botDB.sqlite if not present in the same way before. If an update to the media needs to be done, better to dump the current database in order not to lose the timeout, 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 the assembly. 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
  • press Ctrl+Z, run bg and disown in order to let previous command run in background
  • close your terminal and enjoy