Building a Modern AI Electronic‑Pet Bot with Coze: Design, Implementation, and Workflow
This article walks through the complete process of creating an AI‑driven electronic‑pet bot on the Coze platform, covering concept brainstorming, database design, image‑generation pipelines, Python code snippets, multi‑agent workflow orchestration, and final deployment to the bot store.
The article begins with a brief introduction to a Coze activity that encourages participants to build a bot themed around "childhood" using workflow or image‑stream nodes, and to publish the bot on the Coze store.
Design Draft – Version 1 outlines four core roles (player, pet, item, auto‑task) and lists the technical research topics needed before prototyping.
3. Coze Technical Research
3.1 Database Support explains that Coze does not support file storage, so a relational table must be used. It lists constraints such as single‑SQL per node, no complex joins, no DDL, a maximum of three tables per bot, and a 100‑row limit. An example of simple SQL is provided:
-- 判断表是否为空
SELECT COUNT(*) as record_count FROM player_info;
-- 清空表数据
DELETE FROM player_info;It also shows how to auto‑generate SQL in the database node UI.
3.2 Image Generation describes two use‑cases: generating a pet portrait from a textual description and creating an interactive scene based on that portrait. The workflow uses the "图像流" node with prompt‑optimization and text‑to‑image sub‑nodes. A sample Python formatter for the final message is included:
from datetime import datetime
async def main(args: Args) -> Output:
params = args.params
player_info = params["player_info"][0]
pet_info = params["pet_info"][0]
result_str = ("😄 AI电子宠物孵化成功 🎉🎉\n\n \n\n**🐱宠物信息**:\n - 【名称】{}\n - 【种族】{}\n - 【破壳时间】{}\n - 【饱食度】{}/100\n - 【愉悦度】{}/100\n - 【健康度】{}/100\n - 【重量】{}kg\n\n**👶玩家信息**:\n - 【姓名】{}\n - 【体力】{}/100\n - 【金钱】{}\n").format(
pet_info['image_url'], pet_info["name"], pet_info["appearance"],
datetime.fromtimestamp(pet_info["birth_time"]).strftime("%Y-%m-%d %H:%M:%S"),
pet_info["satiety"], pet_info["mood"], pet_info["health"], pet_info["weight"],
player_info["name"], player_info["power"], player_info['money']
)
return {"result_str": result_str}Additional notes discuss image size trade‑offs (1088×1088 vs 576×576) and the need to add constraints to prevent users from directly manipulating the database via natural language.
4. Design Draft – Version 2 refines gameplay by removing rigid item tables, introducing stamina‑based actions, and simplifying interaction logic.
5. Detailed Implementation
5.1 Pre‑interaction status check workflow calculates health decay over time and updates the pet table. A Python snippet demonstrates the logic:
import time
async def main(args: Args) -> Output:
params = args.params
pet_status = params["pet_status_list"]
interact = params["interact_record_list"]
if not pet_status:
return {"result_str": "😄 你还未创建电子宠物咧,请先创建一只吧~"}
health = int(pet_status[0]["health"])
if health > 0 and interact:
last = interact[0]["create_time"]
diff_hours = (int(time.time()) - last) // 3600
health -= diff_hours * 5
if health <= 0:
return {"result_str": "😳 你的电子宠物已经💀了,请重新创建一只吧~"}
return {"result_str": "Alive", "new_health": health}5.2 Game Agent handles a four‑character idiom guessing mini‑game. It includes workflow steps for opening dialogue, random idiom selection, answer validation, and exit handling. The idiom‑masking code is:
import random
async def main(args: Args) -> Output:
idiom = random.choice(params['idiom_list'])
mask_idx = random.sample(range(4), 2)
mask = ''.join('【】' if i in mask_idx else idiom[i] for i in range(4))
question = f"本轮要猜的成语是 → {mask},用户可以回复完整的四字成语参与"
return {"correct_idiom": idiom, "question": question}5.3 Pet Agent manages pet initialization, status display, interaction feedback, and leaderboard handling. The interaction‑feedback node updates pet attributes based on JSON input and generates an SQL update statement:
import json
async def main(args: Args) -> Output:
p = args.params
changes = json.loads(p['input_str'])
sat = p['satiety'] + changes['satiety']
mood = p['mood'] + changes['mood']
health = p['health'] + changes['health']
weight = p['weight'] + changes['weight']
update_sql = f"UPDATE pet_info SET satiety={sat}, mood={mood}, health={health}, weight={weight} LIMIT 1"
affect = f"【{p['pet_name']}】"
# build affect string omitted for brevity
return {"after_satiety": sat, "after_mood": mood, "after_health": health, "after_weight": weight, "affect_str": affect, "update_sql": update_sql}5.4 Player Agent simply reads the player_info table and formats the result.
5.5 Scheduler Agent routes user intents to the appropriate sub‑agent using prompt rules, e.g., game‑related queries go to Game Agent, pet interactions to Pet Agent, and player queries to Player Agent. Constraints prevent direct natural‑language manipulation of the database.
Finally, the bot is published to the Coze store (link provided) and a short conclusion reflects on the three‑day development effort and invites readers to experiment with complex Coze bots.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.