# WRITE The `WRITE` keyword saves content to files in the bot's drive storage, enabling bots to create documents, export data, and persist information. --- ## Syntax ```basic WRITE content TO "filename" WRITE data TO "filename.csv" AS TABLE WRITE lines TO "filename.txt" AS LINES WRITE content TO "filename" APPEND ``` --- ## Parameters | Parameter | Type | Description | |-----------|------|-------------| | `content` | String | The content to write to the file | | `filename` | String | Path to the file in the bot's storage | | `AS TABLE` | Flag | Write structured data as CSV format | | `AS LINES` | Flag | Write array as separate lines | | `APPEND` | Flag | Add to existing file instead of overwriting | --- ## Description `WRITE` saves content to the bot's configured storage (drive bucket). It supports: - Text files (`.txt`, `.md`, `.json`, `.xml`, `.csv`) - Creating new files or overwriting existing ones - Appending to existing files - Writing structured data as CSV - Automatic directory creation The file path is relative to the bot's storage root. Use forward slashes for subdirectories. --- ## Examples ### Basic File Write ```basic ' Write a simple text file message = "Welcome to our service!" WRITE message TO "welcome.txt" TALK "File saved successfully!" ``` ### Write to Subdirectory ```basic ' Write file to nested folder (directories created automatically) report = "Monthly Report\n\nSales: $10,000\nExpenses: $3,000" WRITE report TO "reports/2025/january.md" ``` ### Write JSON Data ```basic ' Create JSON configuration file config_json = '{"theme": "dark", "language": "en", "notifications": true}' WRITE config_json TO "settings.json" ``` ### Write CSV as Table ```basic ' Export data as CSV - use FIND to get data from database orders = FIND "orders" WHERE status = "completed" LIMIT 100 WRITE orders TO "exports/orders.csv" AS TABLE TALK "Exported " + LEN(orders) + " orders to CSV" ``` ### Write Lines ```basic ' Write array as separate lines log_entries = [ "2025-01-15 10:00 - User logged in", "2025-01-15 10:05 - Order placed", "2025-01-15 10:10 - Payment processed" ] WRITE log_entries TO "logs/activity.log" AS LINES ``` ### Append to File ```basic ' Add entry to existing log file new_entry = FORMAT(NOW(), "YYYY-MM-DD HH:mm") + " - " + event_description + "\n" WRITE new_entry TO "logs/events.log" APPEND ``` --- ## Common Use Cases ### Generate Report ```basic ' Create a formatted report report = "# Sales Report\n\n" report = report + "**Date:** " + FORMAT(NOW(), "MMMM DD, YYYY") + "\n\n" report = report + "## Summary\n\n" report = report + "- Total Sales: $" + FORMAT(total_sales, "#,##0.00") + "\n" report = report + "- Orders: " + order_count + "\n" report = report + "- Average Order: $" + FORMAT(total_sales / order_count, "#,##0.00") + "\n" filename = "reports/sales-" + FORMAT(NOW(), "YYYYMMDD") + ".md" WRITE report TO filename TALK "Report saved to " + filename ``` ### Export Customer Data ```basic ' Export customer list to CSV customers = FIND "customers" WHERE status = "active" WRITE customers TO "exports/active-customers.csv" AS TABLE ' Email the export SEND MAIL "manager@company.com", "Customer Export", "See attached file", "exports/active-customers.csv" ``` ### Save Meeting Notes ```basic ' Save notes from a conversation notes = "# Meeting Notes\n\n" notes = notes + "**Date:** " + FORMAT(NOW(), "YYYY-MM-DD HH:mm") + "\n" notes = notes + "**Participants:** " + participants + "\n\n" notes = notes + "## Discussion\n\n" notes = notes + meeting_content + "\n\n" notes = notes + "## Action Items\n\n" notes = notes + action_items filename = "meetings/" + FORMAT(NOW(), "YYYYMMDD") + "-" + meeting_topic + ".md" WRITE notes TO filename TALK "Meeting notes saved!" ``` ### Create Backup ```basic ' Backup current data data = GET BOT MEMORY "important_data" backup_name = "backups/data-" + FORMAT(NOW(), "YYYYMMDD-HHmmss") + ".json" WRITE JSON_STRINGIFY(data) TO backup_name TALK "Backup created: " + backup_name ``` ### Build Log File ```basic ' Append to daily log log_line = FORMAT(NOW(), "HH:mm:ss") + " | " + user_id + " | " + action + " | " + details log_file = "logs/" + FORMAT(NOW(), "YYYY-MM-DD") + ".log" WRITE log_line + "\n" TO log_file APPEND ``` ### Generate HTML Page ```basic ' Create a simple HTML report html = "\n" html = html + "
Generated: " + FORMAT(NOW(), "YYYY-MM-DD HH:mm") + "
\n" html = html + "