Message a topic to your Telegram chat and get back a gentle bedtime story written for your child, with a picture to go with it.
Uses
ImgBB
OpenAI
Stability AI
Telegram
Send a topic to Telegram, get an illustrated bedtime story
Whenever you message a few words to your Telegram chat, a lost mitten or a dragon scared of the dark, AI writes a short bedtime story for your child by name at their reading level, and a picture of one calm scene is drawn and sent above it. A message too short to name a topic gets a friendly reply asking for one.
The story is written to be read aloud: short sentences, words a child of that age knows, and a calm, sleepy ending. The instructions rule out anything frightening, nobody lost or left alone, no unfriendly creatures, no sad ending, and no real people, brands or places. The picture is drawn from a scene the story itself describes and hosted so Telegram can fetch it. If any part of that fails the story is still sent as text, so a run either shows a real picture or none. Its own story messages are recognised and ignored.
You need a Telegram bot in your chat, the child's name and age, and connections to an image generator and an image host.
incomingMessage.When a new message is received. Save it as incomingMessage.listenerName to blank.Set listenerName to blank.listenerAge to 5.Set listenerAge to 5.ageInYears to listenerAge multiplied by 1.Set ageInYears to listenerAge multiplied by 1.ageInYears is less than 2If ageInYears is less than 2ageInYears is greater than 12If ageInYears is greater than 12chatId to incomingMessage.chat_id.Set chatId to incomingMessage.chat_id.chatId is emptyIf chatId is emptystoryTopic to blank.Set storyTopic to blank.messageErrorIf a step below fails, save the error as messageErrorincomingMessage.text and save it as storyTopic.Trim incomingMessage.text and save it as storyTopic.storyTopic starts with Here is tonight's storyIf storyTopic starts with Here is tonight's storystoryTopic is less than 3If the length of storyTopic is less than 3chatId.Send a message to chat chatId.Tell me what tonight's story should be about - a lost mitten, a dragon who is scared of the dark, anything at all.storyTopic to 300 characters (keeping words whole) and save it as cappedTopic.Shorten storyTopic to 300 characters (keeping words whole) and save it as cappedTopic.promptRole to Write a bedtime story to be read aloud to a child at the end of the day.Set promptRole to Write a bedtime story to be read aloud to a child at the end of the day.
It is for listenerName, who is ageInYears years old. Put listenerName in the story as a character.promptSafety to Keep it gentle. Nothing frightening, no peril, no violence, no injury, no illness, no death, no one lost or left alone for long, and no creature that is not friendly by the end.Set promptSafety to Keep it gentle. Nothing frightening, no peril, no violence, no injury, no illness, no death, no one lost or left alone for long, and no creature that is not friendly by the end.
No romance. No real people, real brands or real places.
Nothing a grown-up would have to explain, and nothing sad at the end.promptStyle to Short sentences and words a child of that age knows. About 350 words. Plain text with no formatting marks. Finish on a calm, sleepy note.Set promptStyle to Short sentences and words a child of that age knows. About 350 words. Plain text with no formatting marks. Finish on a calm, sleepy note.promptPicture to After the story, on its own last line, write PICTURE: followed by one sentence describing a single calm scene from the story that could be drawn. Describe only what is in the picture, and keep it as gentle as the story.Set promptPicture to After the story, on its own last line, write PICTURE: followed by one sentence describing a single calm scene from the story that could be drawn. Describe only what is in the picture, and keep it as gentle as the story.storyPrompt to promptRole@promptSafety@promptStyle@promptPictureTonight's story is about: cappedTopicSet storyPrompt to promptRole@promptSafety@promptStyle@promptPictureTonight's story is about: cappedTopic
.modelResult to nothing.Set modelResult to nothing.modelCallErrorIf a step below fails, save the error as modelCallErrorstoryPrompt. Save it as modelResult.Ask OpenAI: storyPrompt. Save it as modelResult.modelResult to nothing.Change modelResult to nothing.storyRaw to blank.Set storyRaw to blank.storyErrorIf a step below fails, save the error as storyErrormodelResult.response_text and save it as storyRaw.Trim modelResult.response_text and save it as storyRaw.storyRaw to blank.Change storyRaw to blank.storyRaw is emptyIf storyRaw is emptychatId.Send a message to chat chatId.I could not write a story about that tonight. Send me the idea again and I will try once more.storyText to storyRaw.Set storyText to storyRaw.sceneDescription to cappedTopic.Set sceneDescription to cappedTopic.storyRaw contains PICTURE:If storyRaw contains PICTURE:storyRaw by "Split storyRaw by
PICTURE: and save it as storyParts.storyParts and save it as storyText.Trim the first item of storyParts and save it as storyText.storyParts and save it as scenePart.Trim the last item of storyParts and save it as scenePart.scenePart isn't emptyIf scenePart isn't emptysceneDescription to scenePart.Change sceneDescription to scenePart.storyText is emptyIf storyText is emptystoryText to storyRaw.Change storyText to storyRaw.illustrationPrompt to A gentle children's storybook illustration, soft warm colours, friendly and calm, suitable for a young child, with no text or lettering anywhere in the picture. The scene: sceneDescription truncated to 500 characters using ... as the ellipsis (keeping words whole).Set illustrationPrompt to A gentle children's storybook illustration, soft warm colours, friendly and calm, suitable for a young child, with no text or lettering anywhere in the picture. The scene: sceneDescription truncated to 500 characters using ... as the ellipsis (keeping words whole).pictureRef to blank.Set pictureRef to blank.drawingErrorIf a step below fails, save the error as drawingErrorillustrationPrompt. Save it as generated.Generate an image of illustrationPrompt. Save it as generated.text, letters, words, watermark, frightening, dark, scarygenerated.document_ref and save it as pictureRef.Trim generated.document_ref and save it as pictureRef.pictureRef to blank.Change pictureRef to blank.pictureUrl to blank.Set pictureUrl to blank.pictureRef isn't emptyIf pictureRef isn't emptyhostingErrorIf a step below fails, save the error as hostingErrorhosted.Upload an image. Save it as hosted.hosted.url and save it as pictureUrl.Trim hosted.url and save it as pictureUrl.pictureUrl to blank.Change pictureUrl to blank.pictureUrl doesn't start with https://If pictureUrl doesn't start with https://pictureUrl to blank.Change pictureUrl to blank.pictureUrl isn't emptyIf pictureUrl isn't emptychatId.Send a photo to chat chatId.
Photo URL: pictureUrlTonight's story for listenerName: cappedTopic truncated to 900 characters using "..." as the ellipsis (keeping words whole)storyMessage to Here is tonight's story, listenerName.Set storyMessage to Here is tonight's story, listenerName.
storyText truncated to 3600 characters using ... as the ellipsis (keeping words whole).chatId.Send a message to chat chatId.storyMessagepictureUrl is emptyIf pictureUrl is empty