Lingara Lingara ឯកសារ មគ្គុទ្ទេសក៍សិក្សា API បណ្ណាល័យ កម្មវិធី បង្កើត កម្មវិធីលើបណ្ដាញ
ភាសា: ខ្មែរ

ការចាប់ផ្ដើមរហ័ស

ទំព័រនេះត្រូវបានបកប្រែពីភាសាអង់គ្លេស។ ប្រសិនបើទាំងពីរខុសគ្នា ទំព័រភាសាអង់គ្លេសគឺត្រឹមត្រូវ។ អានទំព័រភាសាអង់គ្លេស

ទំព័រនេះនាំកម្មវិធីមួយពីសូន្យ រហូតដល់មានកាតមួយនៅលើគណនីផ្ទាល់ខ្លួនរបស់អ្នក។ គំរូខាងក្រោមធ្វើតាមជំហានដូចគ្នានៅក្នុងភាសានីមួយៗ។

បង្កើតក្លាយអិនត៍ OAuth

កម្មវិធីមួយគឺជាក្លាយអិនត៍ OAuth ដែលមានម៉ានីហ្វេស្ត ដូច្នេះចាប់ផ្ដើមដោយក្លាយអិនត៍មួយ។ បង្កើតវានៅលើទំព័រ «ក្លាយអិនត៍ OAuth» នៃកុងសូល ហើយផ្ដល់ឲ្យវានូវសិទ្ធិ API ដែលកម្មវិធីរបស់អ្នកនឹងត្រូវការ ដូចជាការអានផែនការមេរៀន។ លេខសម្គាល់ក្លាយអិនត៍ (client ID) និងសម្ងាត់របស់វា អនុញ្ញាតឲ្យម៉ាស៊ីនមេរបស់អ្នកហៅ Lingara API ក្នុងនាមជាកម្មវិធីរបស់អ្នក។ រក្សាទុកពួកវានៅក្នុងបរិស្ថាន (environment) របស់ម៉ាស៊ីនមេ។

បង្កើត និងផ្ទុកឡើងម៉ានីហ្វេស្ត

ម៉ានីហ្វេស្តពិពណ៌នាកម្មវិធីរបស់អ្នកទៅកាន់ Lingara៖

  • ឈ្មោះ និងការពិពណ៌នារបស់វា ជាភាសាមេមួយ ព្រមទាំងការបកប្រែដែលជាជម្រើស;
  • render_url ដែលជាអាសយដ្ឋាន HTTPS ដែល Lingara ផ្ញើសំណើទៅ;
  • ស្លតដែលកាតរបស់វាអាចលេចឡើង;
  • បរិបទដែលវាសុំមើល ដែលអ្នករៀនយល់ព្រមនៅពេលដំឡើង (សូមមើល បរិបទ និងការយល់ព្រម);
  • សិទ្ធិ API ដែលវាត្រូវការ ដែលក្លាយអិនត៍ OAuth របស់វាត្រូវតែមានរួចហើយ;
  • ថាតើវាអាចផ្ដល់កំណត់ចំណាំទៅគ្រូឬអត់ (សូមមើល កំណត់ចំណាំសម្រាប់គ្រូ)។

កញ្ចប់ឧបករណ៍បង្កើតម៉ានីហ្វេស្តជា JSON ហើយពិនិត្យវាធៀបនឹងច្បាប់ដែលការផ្ទុកឡើងអនុវត្ត ដូច្នេះកំហុសលេចឡើងនៅលើម៉ាស៊ីនរបស់អ្នកជាមុនសិន។ នៅលើទំព័រ «កម្មវិធី» នៃកុងសូល ជ្រើសរើសក្លាយអិនត៍របស់អ្នក ធ្វើឲ្យវាក្លាយជាកម្មវិធី ហើយផ្ទុកឯកសារឡើង។

បង្កើតសម្ងាត់ចុះហត្ថលេខា

នៅលើទំព័រដដែលនោះ បង្កើតសម្ងាត់ចុះហត្ថលេខាមួយ។ វាត្រូវបានបង្ហាញតែម្ដងប៉ុណ្ណោះ។ ផ្ដល់វាទៅម៉ាស៊ីនមេរបស់អ្នកតាមរយៈបរិស្ថានរបស់វា ហើយកុំ commit វាទៅក្នុងប្រព័ន្ធគ្រប់គ្រងកូដប្រភពឡើយ។ Lingara ចុះហត្ថលេខាលើសំណើនីមួយៗទៅកម្មវិធីរបស់អ្នកដោយប្រើវា ហើយកញ្ចប់ឧបករណ៍បដិសេធសំណើដែលហត្ថលេខាមិនត្រូវគ្នា។ កម្មវិធីមួយអាចមានសម្ងាត់ពីរក្នុងពេលតែមួយ ដូច្នេះអ្នកអាចជំនួសមួយបានដោយគ្មានការផ្អាកសេវា៖ ខណៈពេលដែលសម្ងាត់ពីរកំពុងសកម្ម សំណើនីមួយៗត្រូវបានចុះហត្ថលេខាដោយសម្ងាត់ទាំងពីរ។

សរសេរអនុគមន៍ render

អនុគមន៍ render របស់អ្នកទទួលសំណើមួយ ហើយត្រឡប់កាតមួយ។ សំណើនោះបញ្ជាក់ស្លត ភាសារបស់អ្នករៀន បរិបទដែលពួកគេបានយល់ព្រមចែករំលែក និង subject ដែលជាតម្លៃថេរមួយសម្គាល់អ្នករៀនចំពោះកម្មវិធីរបស់អ្នក។ រក្សាទុកអ្វីៗដែលអ្នករក្សាទុកសម្រាប់អ្នករៀនម្នាក់ៗនៅក្រោម subject។ ប៊ូតុងនៅលើកាតរបស់អ្នកផ្ញើសកម្មភាពមួយ ហើយម៉ាស៊ីនមេរបស់អ្នកឆ្លើយតបវាដោយកាតថ្មីមួយ។ ទំព័រ កាត រាយបញ្ជីអ្វីៗទាំងអស់ដែលកាតអាចផ្ទុកបាន។

ភ្ជាប់ adapter

adapter របស់កញ្ចប់ឧបករណ៍អានសំណើដើម ពិនិត្យហត្ថលេខារបស់វា ហៅអនុគមន៍របស់អ្នក និងសរសេរការឆ្លើយតប។ បម្រើវានៅអាសយដ្ឋានដែល render_url នៃម៉ានីហ្វេស្តរបស់អ្នកកំណត់។ Lingara ហៅតែអាសយដ្ឋាន HTTPS នៅលើអ៊ីនធឺណិតសាធារណៈប៉ុណ្ណោះ។

ដំឡើងវានៅលើគណនីផ្ទាល់ខ្លួនរបស់អ្នក

នៅលើទំព័រ «កម្មវិធី» នៃកុងសូល ជ្រើសរើសដំឡើងកម្មវិធីទៅគណនីរបស់អ្នក ជ្រើសរើសអ្វីដែលវាអាចមើលឃើញ ហើយបញ្ជាក់។ បើកអេក្រង់ផែនការមេរៀននៅលើអេក្រង់ធំ ឬទំព័រដើមនៅលើវេប ហើយកាតរបស់អ្នកនឹងលេចឡើង។

ក្លាយអិនត៍ OAuth នៃកម្មវិធីរបស់អ្នកក៏ទទួលព្រឹត្តិការណ៍ផងដែរ ដូចជាពេលផែនការមេរៀនមួយរួចរាល់។ មគ្គុទ្ទេសក៍ webhooks និងព្រឹត្តិការណ៍ នៅក្នុងឯកសារយោង API ពិពណ៌នាអំពីពួកវា។

ភាសានៃឧទាហរណ៍កូដ

TypeScript

npm install @lingara/apps@0.1.0-alpha.6

Rust

cargo add lingara-apps@0.1.0-alpha.6 --features axum
cargo add axum@0.8
cargo add tokio --features macros,net,rt-multi-thread

Go

go get github.com/Spinning-Cat-Studios/lingara_app_framework_sdk/go@v0.1.0-alpha.6

Java

<dependency>
  <groupId>com.getlingara</groupId>
  <artifactId>lingara-apps-java</artifactId>
  <version>0.1.0-alpha.6</version>
</dependency>

Kotlin

implementation("com.getlingara:lingara-apps-kotlin:0.1.0-alpha.6")

Ruby

gem install lingara-apps -v 0.1.0-alpha.6

PHP

composer require spinningcatstudios/lingara-apps:0.1.0-alpha.6 spinningcatstudios/lingara:@alpha
ភាសានៃឧទាហរណ៍កូដ

TypeScript

import { manifest } from "@lingara/apps";

const json = manifest()
  .defaultLocale("en")
  .name({ en: "Daily five", "zh-Hans": "每日五词" })
  .description("Five words to review, picked from your plan.")
  .renderUrl("https://apps.example.com/lingara/render")
  .slots("home.side", "plans.empty_detail")
  .context("languages", "plan_summary")
  // `.scopes(…)` lists the API scopes your client uses, for the learner's consent page. This app uses none.
  .tutorNote()
  .toJson(); // Throws ManifestError naming the rule an upload would refuse.

writeFileSync("manifest.json", json); // Upload it in the console.

Rust

let manifest = manifest()
    .default_locale("en")
    .name("Daily five")
    .description("Five words to review, picked from your plan.")
    .render_url("https://apps.example.com/lingara/render")
    .slots([AppSlotName::HomeSide, AppSlotName::PlansEmptyDetail])
    .context([ContextSliceKind::Languages, ContextSliceKind::PlanSummary])
    // `.scopes(…)` lists the API scopes your client uses, for the learner's consent page. This app uses none.
    .tutor_note(true)
    .build()?;
std::fs::write("manifest.json", manifest.to_json())?;

Go

manifest, err := lingaraapps.NewManifest().
	DefaultLocale("en").
	Names(map[string]string{"en": "Daily five", "zh-Hans": "每日五词"}).
	Description("Five words to review, picked from your plan.").
	RenderURL("https://apps.example.com/lingara").
	Slots(lingaraapps.AppSlotNameHomeSide, lingaraapps.AppSlotNamePlansEmptyDetail).
	Context(lingaraapps.ContextSliceKindLanguages, lingaraapps.ContextSliceKindPlanSummary).
	// `.Scopes(…)` lists the API scopes your client uses, for the learner's consent page. This app uses none.
	TutorNote(true).
	Build()
if err != nil {
	return err // a *lingaraapps.ManifestError names the broken rule
}
data, err := manifest.ToJSON()
if err != nil {
	return err
}
// Upload manifest.json in the console.
err = os.WriteFile("manifest.json", data, 0o644)

Java

import com.getlingara.apps.Manifest;
import com.getlingara.apps.model.AppSlotName;
import com.getlingara.apps.model.ContextSliceKind;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

String json =
    Manifest.manifest()
        .defaultLocale("en")
        .name("Daily five")
        .description("Five words to review, picked from your plan.")
        .renderUrl("https://apps.example.com/lingara/render")
        .slots(AppSlotName.HOME_SIDE)
        .context(ContextSliceKind.LANGUAGES, ContextSliceKind.PLAN_SUMMARY)
        // `.scopes(…)` lists the API scopes your client uses, for the learner's consent
        // page. This app uses none.
        .tutorNote(true)
        .build() // refuses what the upload would, naming the rule
        .toJson();
// Upload manifest.json in the console, beside the app's icon.
Files.writeString(Path.of("manifest.json"), json);

Kotlin

import com.getlingara.apps.kotlin.manifest
import com.getlingara.apps.kotlin.model.AppSlotName
import com.getlingara.apps.kotlin.model.ContextSliceKind
import java.io.File

val json =
    manifest {
        defaultLocale = "en"
        name("Daily five")
        description("Five words to review, picked from your plan.")
        renderUrl = "https://apps.example.com/lingara/render"
        slots(AppSlotName.HOME_PERIOD_SIDE)
        context(ContextSliceKind.LANGUAGES, ContextSliceKind.PLAN_SUMMARY)
        // `scopes(…)` lists the API scopes your client uses, for the learner's consent page.
        // This app uses none.
        tutorNote = true
    }.toJson() // refuses what the upload would, naming the rule
// Upload manifest.json in the console, beside the app's icon.
File("manifest.json").writeText(json)

Ruby

manifest = Lingara::Apps.manifest
  .default_locale("en")
  .name("Daily five") # A bare String is the default locale's; or {"en" => …, "zh-Hans" => …}.
  .description("Five words to review, picked from your plan.")
  .render_url("https://apps.example.com/lingara")
  .slots("home.side", "plans.empty_detail")
  .context("languages", "plan_summary")
  # `.scopes(…)` lists the API scopes your client uses, for the learner's consent page. This app uses none.
  .tutor_note
# Raises Lingara::Apps::ManifestError naming the rule the upload would refuse.
File.write("manifest.json", manifest.to_json)

PHP

use Lingara\Apps\Generated\AppSlotName;
use Lingara\Apps\Manifest;

$json = Manifest::create()
    ->defaultLocale('en')
    ->name(['en' => 'Daily five', 'zh-Hans' => '每日五词'])
    ->description('Five words to review, picked from your plan.')
    ->renderUrl('https://apps.example.com/lingara/render')
    ->slots(AppSlotName::HOME_SIDE, AppSlotName::PLANS_EMPTY_DETAIL)
    ->context('languages', 'plan_summary')
    // `->scopes(…)` lists the API scopes your client uses, for the learner's consent page. This app uses none.
    ->tutorNote()
    ->toJson(); // Throws ManifestException naming the rule an upload would refuse.

file_put_contents('manifest.json', $json); // Upload it in the console.
ភាសានៃឧទាហរណ៍កូដ

TypeScript

const app = lingaraApp({
  // lgr_whsec_…, from the app's settings; pass two during a rotation.
  secret: process.env.LINGARA_APP_SECRET!,
  // Hold per-learner state on `subject`, the same value your events carry.
  render: (request) => (request.slot === "home.side" ? withNote(request) : todayCard(streaks.get(request.subject) ?? 0)),
  actions: {
    // The button's `action`; may arrive twice, so make it safe to repeat.
    done: (request) => {
      streaks.set(request.subject, (streaks.get(request.subject) ?? 0) + 1);
      return todayCard(streaks.get(request.subject)!);
    },
  },
});

Rust

// The app's signing secret (lgr_whsec_…), from the environment.
let seen = Seen::default();
let (render_seen, action_seen) = (Arc::clone(&seen), Arc::clone(&seen));
let app = App::new([std::env::var("LINGARA_APP_SECRET")?], move |request: AppRenderRequest| {
    render(Arc::clone(&render_seen), request)
})?
// The button's `action` comes back as `action_id`. An action can arrive
// twice, so keep it safe to repeat.
.action("next", move |request: AppActionRequest| {
    let seen = Arc::clone(&action_seen);
    async move {
        *seen.lock().map_err(|_| "poisoned")?.entry(request.subject).or_default() += 1;
        today_card("Next word", "zh")
    }
});

Go

// Per-learner state is keyed on Subject, the lgr_sub_… value the app's
// events carry too.
var mu sync.Mutex
streaks := map[string]int{}

render := func(ctx context.Context, req lingaraapps.AppRenderRequest) (lingaraapps.Replier, error) {
	mu.Lock()
	days := streaks[req.Subject]
	mu.Unlock()
	return lingaraapps.NewCard().
		Heading("Daily five", 1).
		Progress(float64(days%5)/5, "This week").
		Button("Done today", "done").
		Build()
}
// An action may arrive twice: make it safe to run twice.
done := func(ctx context.Context, req lingaraapps.AppActionRequest) (lingaraapps.Replier, error) {
	mu.Lock()
	streaks[req.Subject]++
	mu.Unlock()
	return lingaraapps.NewCard().Heading("Nice work", 1).Text("See you tomorrow.").Build()
}
app := lingaraapps.App{
	Render:  render,
	Actions: map[string]lingaraapps.ActionFunc{"done": done},
}

Java

import com.getlingara.apps.Card;
import com.getlingara.apps.LingaraApp;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// Per-learner state is keyed on subject, the value the app's events carry too.
Map<String, Integer> seen = new ConcurrentHashMap<>();
LingaraApp app =
    LingaraApp.builder()
        // The app's signing secret (lgr_whsec_…); pass two during a rotation.
        .secrets(System.getenv("LINGARA_APP_SECRET"))
        .render(
            request -> {
              int count = seen.getOrDefault(request.getSubject(), 0);
              return Card.card()
                  .heading("Today's five", 1)
                  .text("Reviewed " + count + " so far.")
                  .button("Next", "next")
                  .build();
            })
        // The button's action comes back as the request's action_id. It may arrive
        // twice, so make it safe to repeat.
        .action(
            "next",
            request -> {
              int count = seen.merge(request.getSubject(), 1, Integer::sum);
              return Card.card().progress(Math.min(1.0, count / 5.0), count + " of 5").build();
            })
        .build();

Kotlin

import com.getlingara.apps.kotlin.LingaraApp
import com.getlingara.apps.kotlin.card
import com.getlingara.apps.kotlin.lingaraApp
import java.util.concurrent.ConcurrentHashMap

// Per-learner state is keyed on subject, the value the app's events carry too.
val seen = ConcurrentHashMap<String, Int>()
val app =
    lingaraApp {
        // The app's signing secret (lgr_whsec_…); pass two during a rotation.
        secrets(System.getenv("LINGARA_APP_SECRET"))
        render { request ->
            val count = seen.getOrDefault(request.subject, 0)
            card {
                heading("Today's five", 1)
                text("Reviewed $count so far.")
                button("Next", "next")
            }
        }
        // The button's action comes back as the request's action_id. It may arrive twice,
        // so make it safe to repeat.
        action("next") { request ->
            val count = seen.merge(request.subject, 1, Int::plus) ?: 1
            card { progress(minOf(1.0, count / 5.0), "$count of 5") }
        }
    }

Ruby

# lgr_whsec_…, from the app's settings; pass an Array of two during a rotation.
app = Lingara::Apps::App.new(secret: ENV.fetch("LINGARA_APP_SECRET"))
# Hold per-learner state on `subject`, the same value your events carry.
app.render do |request|
  (request.slot == "home.side") ? with_note(request) : today_card(STREAKS[request.subject])
end
# The button's `action`; it may arrive twice, so make it safe to repeat.
app.action("done") do |request|
  STREAKS[request.subject] += 1
  today_card(STREAKS[request.subject])
end

PHP

use Lingara\Apps\App;
use Lingara\Apps\Generated\AppActionRequest;
use Lingara\Apps\Generated\AppRenderRequest;

$app = new App(
    // lgr_whsec_…, from the app's settings; pass a list of two during a rotation.
    secret: (string) getenv('LINGARA_APP_SECRET'),
    // Hold per-learner state on `subject`, the same value your events carry.
    render: static fn(AppRenderRequest $request) => $request->getSlot() === AppSlotName::HOME_SIDE
        ? withNote($request)
        : todayCard($streaks[$request->getSubject()] ?? 0),
    actions: [
        // The button's `action`; may arrive twice, so make it safe to repeat.
        'done' => static function (AppActionRequest $request) use (&$streaks): CardModel {
            $streaks[$request->getSubject()] = ($streaks[$request->getSubject()] ?? 0) + 1;
            return todayCard($streaks[$request->getSubject()]);
        },
    ],
);
ភាសានៃឧទាហរណ៍កូដ

TypeScript

// A node:http request listener: it reads the raw body, verifies, dispatches and replies.
createServer(nodeHandler(app)).listen(8787);

Rust

// Nest the adapter at the path your manifest's render_url names.
let routes = axum::Router::new().nest("/lingara/render", lingara_apps::axum::router(app));
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, routes).await?;

Go

// The app's signing secret, lgr_whsec_…, or both during a rotation.
handler, err := lingaraapps.NewHandler(app, os.Getenv("LINGARA_APP_SECRET"))
if err != nil {
	return err // a malformed secret fails here, at startup
}
mux := http.NewServeMux()
mux.Handle("/lingara", handler)
err = http.ListenAndServe(":8080", mux)

Java

import com.getlingara.apps.JdkHttpHandler;
import com.getlingara.apps.LingaraApp;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;

// The JDK's own server: the handler reads the raw body, verifies it, then dispatches.
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/lingara/render", JdkHttpHandler.of(app));
server.start();

Kotlin

import com.getlingara.apps.kotlin.LingaraApp
import com.getlingara.apps.kotlin.ktor.lingaraApp
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.server.routing.route
import io.ktor.server.routing.routing

// Route.lingaraApp reads the raw body itself, so content negotiation never parses it.
embeddedServer(CIO, port = 8080) {
    routing {
        route("/lingara/render") { lingaraApp(app) }
    }
}.start(wait = true)

Ruby

# config.ru: any Rack server (Puma, Falcon, rackup) runs it. It reads the
# raw body itself, so mount it where nothing has parsed the JSON first.
run Lingara::Apps::RackApp.new(app)

PHP

use Lingara\Apps\Psr15Handler;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;

// A PSR-15 handler: mount it on your framework's route, or answer from a
// front controller as here. It reads the raw body, verifies, dispatches and replies.
$factory = new Psr17Factory();
$handler = new Psr15Handler($app, $factory, $factory);
$response = $handler->handle((new ServerRequestCreator($factory, $factory, $factory, $factory))->fromGlobals());
http_response_code($response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
    header("{$name}: " . implode(', ', $values));
}
echo $response->getBody();

មើលផងដែរ