{"id":15592160,"url":"https://github.com/derhuerst/chatbot-coroutine","last_synced_at":"2025-10-13T01:03:28.885Z","repository":{"id":57197262,"uuid":"94682606","full_name":"derhuerst/chatbot-coroutine","owner":"derhuerst","description":"Model chatbots as conversations.","archived":false,"fork":false,"pushed_at":"2020-05-03T15:52:09.000Z","size":32,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-25T11:00:55.883Z","etag":null,"topics":["bot","chat","chatbot","conversations","messenger"],"latest_commit_sha":null,"homepage":"https://github.com/derhuerst/chatbot-coroutine","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derhuerst.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-18T11:22:18.000Z","updated_at":"2020-05-03T15:52:02.000Z","dependencies_parsed_at":"2022-09-16T13:10:49.700Z","dependency_job_id":null,"html_url":"https://github.com/derhuerst/chatbot-coroutine","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/derhuerst/chatbot-coroutine","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fchatbot-coroutine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fchatbot-coroutine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fchatbot-coroutine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fchatbot-coroutine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derhuerst","download_url":"https://codeload.github.com/derhuerst/chatbot-coroutine/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derhuerst%2Fchatbot-coroutine/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264721531,"owners_count":23653949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","chat","chatbot","conversations","messenger"],"created_at":"2024-10-02T23:56:13.016Z","updated_at":"2025-10-13T01:03:23.845Z","avatar_url":"https://github.com/derhuerst.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chatbot-coroutine\n\n**Model chatbots as conversations.** A [coroutine](https://en.wikipedia.org/wiki/Coroutine) that allows you to write chatbot conversations using [generator functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator).\n\n[![npm version](https://img.shields.io/npm/v/chatbot-coroutine.svg)](https://www.npmjs.com/package/chatbot-coroutine)\n[![build status](https://img.shields.io/travis/derhuerst/chatbot-coroutine.svg)](https://travis-ci.org/derhuerst/chatbot-coroutine)\n![ISC-licensed](https://img.shields.io/github/license/derhuerst/chatbot-coroutine.svg)\n[![chat on gitter](https://badges.gitter.im/derhuerst.svg)](https://gitter.im/derhuerst)\n\n\n## Rationale\n\nHave you ever wrote chatbot code looking like this?\n\n```js\nconst data = {}\n\nmyChatbot.on('message', (user, msg) =\u003e {\n\tif (!data[user]) data[user] = {state: 0}\n\tconst d = data[user]\n\n\tif (d.state === 0) {\n\t\td.state = 1\n\t\tbot.send(user, 'Tell me foo!')\n\tif (d.state === 1) {\n\t\td.foo = msg\n\t\td.state = 2\n\t\tbot.send(user, 'Tell me bar!')\n\t} else if (d.state === 2) {\n\t\td.state = 0\n\t\tbot.send(user, `foo: ${foo} bar: ${msg}`)\n\t}\n\n\tdelete data[user]\n})\n```\n\nNow **this example is still very naive**, as it doesn't handle invalid input and keeps data only in memory. But it demonstrates how such a structure can get quickly get very complex. Using `chatbot-coroutine`, you can **model your converstional bot like you would reason about it: As one coherent flow of incoming and outgoing messages**.\n\n```js\nconst createRespond = require('chatbot-coroutine')\nconst inMemStorage = require('chatbot-coroutine/in-mem-storage')\n\nconst conversation = function* (ctx) {\n\tlet foo = yield ctx.prompt('Tell me foo!')\n\tlet bar = yield ctx.prompt('Tell me bar!')\n\tyield ctx.send(`foo: ${foo} bar: ${bar}`)\n\n\tyield ctx.clear()\n}\n\nconst respond = createRespond(inMemStorage, bot, conversation, console.error)\nbot.on('message', respond)\n```\n\nTo make the code handle crashes, write it like this:\n\n```js\nconst conversation = function* (ctx) {\n\tlet foo = yield ctx.read('foo')\n\tif (!foo) {\n\t\tfoo = yield ctx.prompt('Tell me foo!')\n\t\tyield ctx.write('foo', foo)\n\t}\n\n\tlet bar = yield ctx.read('bar')\n\tif (!bar) {\n\t\tbar = yield ctx.prompt('Tell me bar!')\n\t\tyield ctx.write('bar', bar)\n\t}\n\n\tyield ctx.send(`foo: ${foo} bar: ${bar}`)\n\tyield ctx.clear()\n}\n```\n\n\n## Installing\n\n```shell\nnpm install chatbot-coroutine\n```\n\n\n## Usage\n\nWrite a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) that represents the conversation the bot will have. See [Rationale](#rationale) for an example.\n\n```js\nconst conversation = function* (ctx, user) {\n\t// instructions\n}\n```\n\nUsing `createRespond`, create a `respond` function from it. Pass in a storage adapter that fits your needs.\n\n```js\nconst createRespond = require('chatbot-coroutine')\nconst inMemStorage = require('chatbot-coroutine/in-mem-storage')\n\nconst respond = createRespond(inMemStorage, bot, conversation, console.error)\nbot.on('message', respond)\n```\n\n### storage adapters\n\n```js\nconst inMemStorage = require('chatbot-coroutine/in-mem-storage')\nconst levelDBStorage = require('chatbot-coroutine/leveldb-storage')(db)\n```\n\n## API\n\n### `createRespond(storage, bot, conversation, onError)`\n\n`bot` should have a `send(user, msg)` method.\n\n`conversation` should be a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) function, taking two arguments:\n\n- `ctx`: the context, allowing you to interact with the bot and the storage\n- `user`: the current user ID\n\n### `ctx.msg()`\n\nWaits for a message from the user.\n\n### `ctx.send(msg)`\n\nSends a message to the user.\n\n### `ctx.prompt(msg)`\n\nShorthand for `ctx.send(msg)` + `ctx.msg()`.\n\n### `ctx.write(key, val)`\n\nWrites `val` at `key` to the storage. This is namespaced by the user.\n\n### `ctx.read(key)`\n\nReads the value at `key` from the storage. This is namespaced by the user.\n\n### `ctx.clear()`\n\nDeletes everything in the storage. This is namespaced by the user.\n\n\n### implementing a storage adapter\n\n`storage` should accept a `user` (working like a namespace) and have the following methods:\n\n- `storage(user).write(key, val)` -\u003e `Promise` – should encode JSON\n- `storage(user).read(key)` -\u003e `Promise` – should decode JSON\n- `storage(user).clear()` -\u003e `Promise`\n\n\n## Contributing\n\nIf you have a question or have difficulties using `chatbot-coroutine`, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to [the issues page](https://github.com/derhuerst/chatbot-coroutine/issues).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhuerst%2Fchatbot-coroutine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderhuerst%2Fchatbot-coroutine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderhuerst%2Fchatbot-coroutine/lists"}