How to Create a Bot for SimpleX-Chat with TypeScript

Background

SimpleX Chat, which is the first messenger without user IDs and privacy-focused, is also an open source project and you could self host the server as you like. But it's not many bot tutorials and deploy it simply. As a blog said, it has to use CLI to be gateway/bridge to communicate between SMP Server and your bot server.

Architecture

What I Expected

I want to put the CLI to the docker container so I could deploy it easily.

But I faced the issue that I need to input the username to create user profile when running the CLI at the first time, and there is no CLI option to set it. To make matters worse, some APIs like create group didn't work in typescript SDK.

What I did

So I forked the repo to fix the problems of CLI and SDK, and created a repo to show how to dockerize the CLI and build a bot server.

Dockerize CLI

I add --display-name option to the CLI so I could create user profile with non-interactive mode. I created a docker image named feimeizhan/simplex-chat-gateway which code is here.

Typescript SDK

I fixed the issue like create group error and no group list, and publish as @reply2future/simplex-chat which code is here

bot-center

I also created a bot example to show how to use typescript sdk.

How to Use simplex-chat-gateway

Requirements

  1. docker and docker-compose
  2. Linux system

Steps

  1. Run the comand git clone https://github.com/reply2future/simplex-chat-gateway.git to clone the repo and cd to the directory of repo.
  2. Copy .env.template to .env in current directory, and change the environment variables as you need.
  3. Run the command docker compose up -d
  4. You could see the bot address and groupId in the console of docker container bot-center like below:
  5. You need to add the bot by paste the bot address to your simplex-chat client, and the bot accepts the friend request automatically, and you could see the contact_id of your user profile.
  6. Send a message like /square-num 8 which is pre-defined handler in bot-center, then the bot would reply 8 * 8 = 64
  7. (Optional) add you to the pre-created group like Notification by calling the api of bot-center like http://localhost:6794/invite/:contact_id, the more information you could see the code here, so you could send message to the group from bot-center by using api like http://localhost:6794/notification/:group_id.

How to Create your handler in bot-center

I will show it how to add your handler to bot-center

Requirements

TypeScript

Steps

  1. Go to the bot-center directory
  2. Create a handler in ./handlers directory, like ./handlers/direct/echo.ts
  3. Implement interface IHandler in echo.ts
  4. Add echo.ts to ./handlers/index.ts, like ['echo', new EchoHandler()]
  5. Deploy your bot-center
  6. Test it by sending a direct message to bot like /echo test

More SDKs

The official supported SDK is TypeScript, but you could create your SDK using your familiar programming language, because it uses websocket.

Why no windows/macOS

Because of the secure reason the SimpleX-Chat agent will bind 127.0.0.1 only, and network_mode: host is not supported in a meaningful way on macOS and Windows due to Docker's reliance on a VM architecture, so it doesn't work in macos and windows.

Ref

  1. Exploring Simplex.Chat, A Scriptable, Decentralized Messaging App
  2. SimpleX Chat terminal (console) app for Linux/MacOS/Windows
  3. simplex-chat-gateway repo
  4. what is the difference of network_mode: host between linux and macos/windows?