/Dialute

🧶 The fastest way to build an app for SberSalute

Primary LanguageTypeScriptGNU General Public License v3.0GPL-3.0

🧶 Dialute

The way you will want to create Chat and Canvas Apps for SberSalute. Use the power of generator based scripts and simple interface of the framework. Based on SmartApp API

⚙️ Install

The framework is published in NPM, thus you can simply install it into your existing

$: npm install dialute

The preferred way to initialize the project is to clone needed template and immediately start development.

$: git clone https://github.com/Dikower/dialute-starter-svelte
# or simply
$: npx degit dikower/dialute-starter-svelte

🚀 Startup

Dialute uses Express behind to handle requests from SmartMarket. Thanks to the framework, you can skip all boring and routine stuff about parsing request bodies and managing sessions of users and focus on logic and dialog creation using JavaScript Generators. If you are using example or template and familiar with ngrok, just run

# Shell №1
$: npm run dev

#-------------
# Shell №2
$: ngrok http 8000

💎 Concept

The generator represents a dialog. When a user sends a message it triggers the update of SberRequest object, that is passed to the generator as reference. That's why you can easily read the updated message from the same variable. On the other hand, yield will return the content, you pass to user, and then will wait until next message to continue executing script. The generator saves context inside, so it helps us to construct state dependent dialog using just plain JavaScript or TypeScript

  • Import the framework starter and SberRequest for type annotation

    import { Dialute, SberRequest } from 'dialute';
  • Write a generator, that describes the dialog. It must have one required argument: SberRequest and one optional argument Context, you can

      // You can use any name for generator you want
      // r: this is a ref for SberRequest, it will be updated after each user message
      // ctx: Optional argument for advanced techniques, you can skip it for now
      // function* script(r: SberRequest, ctx: any) {
      function* script(r: SberRequest) { // Also possible
        while (true) {
          // send the message to user 
          yield 'Hello world from Dialute!';
          // yield r.msg  // This will make echo-bot, that repeats user input
        }
      }
  • Set the entrypoint of dialog and start the server

    // Creates ready to start server from generator
    const d = Dialute.fromEntrypoint(script as GeneratorFunction);
    // Starts listening for requests from SmartMarket
    d.start();
  • Use nodemon in package.json scripts to enable hot reload

    "scripts": {
      "dev": "nodemon --exec npx ts-node src/index.ts",
    }
  • Run ngrok tunnel on port 8000. Paste https link into Webhook field in SmartApp settings.

    $: ngrok http 8000
  • The whole code can be found inside example directory

📋 Docs

https://dialute.vercel.app

🥰 Support

You can always click on the ⭐️ of the repo and donate. I will be very glad for any support

"Buy Me A Coffee"