Choose Your Own Adventure Bot Walkthrough in TypeScript - Learn Temporal
Choose Your Own Adventure Bot Walkthrough in TypeScript - Learn Temporal
WORK IN PROGRESS
This tutorial is a work in progress. Some sections may be incomplete, out of date, or
missing. We're working to update it.
Introduction
In this tutorial, we'll integrate all the knowledge gained from Core and Logging APIs in an end-
to-end, complete demo application - which happens to be a Choose Your Own Adventure
game that you can play on Discord or Slack!
This project will integrate and give context to your understanding of Temporal SDK APIs:
logging with Sinks, Activity dependency injection, Timer and Promise.race design patterns,
Signals (and HTTP Servers for them), Polling patterns, and continueAsNew for indefinitely
long running Workflows.
SKIP AHEAD
View the completed project on GitHub: https://github.com/JoshuaKGoldberg/temporal-
adventure-bot
https://learn.temporal.io/tutorials/typescript/chatbot/ 1/4
02/08/2023, 06:51 Choose Your Own Adventure Bot walkthrough in TypeScript | Learn Temporal
Prerequisites
Set up a local development environment for developing Temporal applications using
TypeScript
Review the Hello World in TypeScript tutorial to understand the basics of getting a
Temporal TypeScript SDK project up and running.
Project Requirements
On /instructions , posts instructions to Slack/Discord and pins the message
Continuously runs the game until it reaches an end state:
Every day, post the current entry as a poll
Wait until the earlier of:
Every day, check the poll results
If there is consensus, determine next state
If no consensus, remind people to vote
Allow an admin to /force a choice any time
Report important game updates to a specified logger
https://learn.temporal.io/tutorials/typescript/chatbot/ 2/4
02/08/2023, 06:51 Choose Your Own Adventure Bot walkthrough in TypeScript | Learn Temporal
Overview
Worker
The Temporal Worker is set up in src/worker.ts . It uses two common Temporal patterns:
Dependency Injection: using the integration object created by createIntegration to
provide APIs for the social platform being targeted ( Discord or Slack ) (see Platforms)
Logging Sinks: providing a logger.sink method for the Workflows to log out to
console.log
Client
The client in src/client.ts will ask Temporal to run two different Workflows:
1. instructions : Posts instructions to the social platform and pins the message
2. runGame : Continuously runs the game state until the game is finished
runGame
Each iteration of the game (so, daily), runGame goes through these steps:
1. If the entry has no options, the game is over
2. Post the current entry as a poll
3. Check and remind people to vote once a day until either...
...a choice is made by consensus
...an admin forces a choice
4. If the choice was forced by an admin, mention that
https://learn.temporal.io/tutorials/typescript/chatbot/ 3/4
02/08/2023, 06:51 Choose Your Own Adventure Bot walkthrough in TypeScript | Learn Temporal
The platformFactory function used in both workers and Workflows reads from
process.env to return the createIntegration and createServer methods for the social
platform being targeted.
Integrations
createIntegration : creates the client API used to send messages to the social platform.
For example, the Slack integration uses the Slack Bolt SDK.
Servers
createServer creates the (generally Express) server that runs locally and receives webhook
events from the social platform. Both the Discord and Slack servers use Ngrok to expose a
local port on the public web, so that a /force command configured on the platform sends a
message, it can Signal to the Workflow.
https://learn.temporal.io/tutorials/typescript/chatbot/ 4/4