In addition to Twit, we'll also need Tracery, which is required Tracery-Grammar. To create a story with Tracer, you need to create a grammar, which is a specific type of format. We'll say var grammar = tracery.createGrammar.
This is just JSON data. Let's make a simple story to start. We'll have a character, and let's make a Kafkaesque bot. We will say our character is either K, the inspector, the director, the lawyer, or the officer. We'll have an action, we'll say walk, stroll, meander.
We need a place, office, bank, court. We'll have an object, so paper, bribe, official. We need an origin, which is the starting symbol for our story. We create a structure here that incorporates all of our variables.
We'll say character, and we wrap these in the hash symbol, so character action to the place for the object. To get a story from this, we say grammar.flatten and then pass it our starting symbol, which is origin. If we log out our story, K strolled to the bank for the official.
We do it again, the officer walked to the court for the paper. That's a good start, but what are we missing? First, there're some modifiers we can add. We can say character.capitalize() if we always want that word to be upper case.
What else is weird? The officer walked to the court. Well, we want action to be in the past, so we can add another modifier here, action.ed() and then down here, we'll say grammar.addModifiers(tracery.baseEngModifiers).
Now if we run this, the officer walked to the bank for the official. The director meandered to the bank for the paper. The officer meandered to the office for the paper, etc. Object.a() will add a or an in front of our object.
The inspector meandered to the bank for a bribe. K walked to the court for an official. We can also say object.s(), say to get some, and object.s() will pluralize our variable. The lawyer strolled to the court to get some officials. The inspector meandered to the bank to get some bribes.
Let's create a slightly more complex story. Let's have three characters. We'll come back to actions in a minute. We can keep our place. Let's change this up a little bit.
Let's delete this for now. Here we're going to nest variables a little bit, and we're going to have a story variable, which is the structure of the story. Here, we're going to have a setJob variable, and this will tie two variables together. For example, if the job is a lawyer, the job's actions could be argued in court, and filed some paperwork.
If the job is the inspector, the actions might be talked with the lawyer and conducted meetings. The third job could be an officer, who arrested people, or stood in the courtroom.
In origin, we're going to have our story variable. We'll say setJob to pick the job, and we'll say that the protagonist variable is the character. This will pick a character and a job, and fill these in with the story structure. For our story, let's say protagonist the job, went to the place every day, usually they actions. Let's run that.
Oops, spelled protagonist wrong. Try again. I'm also missing another hash after setJob, and I'm also missing an actions here.
There's usually some troubleshooting involved in generating these stories. Once it works, you can generate a ton of mini stories. Karl, the officer, went to the office every day, usually they arrested people. Karl, the lawyer, went to the court every day, usually they filed some paperwork.
We can do one more thing to make the story a little more personal, and that is setPronouns. We need pronouns in a couple different settings. We need they, like they went to the store, which will be they. We need them, like he talked to them. We need their, like their coffee mug, and we need theirs, like it was theirs.
Let's do that for he and she too. Remember to add the comma. We want to add our setPronouns in our origin. In our story, let's say, then they picked up their object.
Ada, the lawyer, went to the court every day, usually they argued in court, then she picked up her letter. Let's loop through our story for other pronouns, and replace this one with the variable. If we run this, Karl, the lawyer, went to the office every day. Usually he filed some paperwork, then he picked up his bribe.
Aiden, the inspector, went to the office every day. Usually, he talked with the lawyers, then he picked up his bribe. It's consistent, and now, we have a story generator.
From here, all we have to say is bot.post Status/update with our status which is our story, error data response, if there's an error, log it out. Otherwise, bot has tweeted plus our story. We can see that our story has been tweeted.