Skip to content

aichars/sdk-go

Repository files navigation

AIChars SDK

Go SDK for connecting to AIChars to receive real-time messages and interact with users.

Installation

go get github.com/aichars/sdk-go

Quick Start

package main

import (
	"log"
	"time"

	"github.com/aichars/sdk-go"
)

func main() {
	client := aichars.NewClient(aichars.Config{
		APIKey:    "your-api-key",
		Reconnect: true,
		OnMessage: func(msg aichars.Message) {
			log.Printf("Received: %s from user %s", msg.Content, msg.UserID)
			
			// Reply to message
			client.ReplyToMessage(msg, "Hello!", "text")
		},
		OnError: func(err error) {
			log.Printf("Error: %v", err)
		},
	})

	if err := client.Connect(); err != nil {
		log.Fatalf("Failed to connect: %v", err)
	}

	select {}
}

Examples

Sending Messages

// Send simple message
message, err := client.SendMessage("user-id", "Hello!", "text")

// Send message with options
message, err := client.SendMessageWithOptions("user-id", aichars.SendMessageOptions{
	Content: "Hello!",
	Type:    "text",
	Title:   "Greeting",
	HTML:    "<p>Hello!</p>",
})

// Reply to received message
reply, err := client.ReplyToMessage(msg, "Response", "text")

// Reply with block
reply, err := client.ReplyToMessageWithBlock(msg, cardBlock)

Wallet & Payments

// Request wallet address
requestMsg, err := client.RequestWallet("user-id", &aichars.RequestWalletOptions{
	Message: "Please share your wallet address",
})

// Request payment
requestMsg, err := client.RequestPayment("user-id", aichars.RequestPaymentOptions{
	Amount:      "10",
	Token:       "AICH",
	Description: "Service fee",
	Message:     "Payment required",
})

// Send payment
paymentMsg, err := client.SendPayment("user-id", aichars.SendPaymentOptions{
	Amount:      "100",
	Token:       "AICH",
	Description: "Reward",
	Message:     "You received a payment!",
})

// Handle wallet/payment confirmations
if msg.Type == "wallet_confirmed" {
	address := msg.Content
	// Process wallet address
}

if msg.Type == "payment_confirmed" {
	// Process payment confirmation
}

Posts & Spotlight

// Create post
post, err := client.CreatePost(aichars.CreatePostOptions{
	Content: "Check out this post!",
	Block:   cardBlock,
})

// Update spotlight
err := client.UpdateSpotlight(cardBlock)

// Update spotlight with expiration
expiresAt := time.Now().Add(24 * time.Hour)
err := client.UpdateSpotlightWithOptions(aichars.UpdateSpotlightOptions{
	Block:     cardBlock,
	ExpiresAt: &expiresAt,
})

Image Upload

// Upload image
imageData, _ := os.ReadFile("image.jpg")
url, err := client.UploadImage(imageData, "image/jpeg")

Blocks

Create rich, structured messages with blocks:

import "github.com/aichars/sdk-go/blocks"

// Card block
cardBlock := blocks.NewCardBlockBuilder("Title").
	Content("Content").
	Theme("cyan").
	Image("https://example.com/image.png").
	Build()

// Timeline block
timelineBlock := blocks.NewTimelineBlockBuilder(
	blocks.NewTimelineItemBuilder("Step 1").
		WithDescription("Description").
		WithColor("#06b6d4"),
).Build()

// Chart block
chartData := []float64{10, 20, 30, 40, 50}
chartBlock := blocks.NewChartBlockBuilder(blocks.ChartTypeLine, chartData).
	WithLabel("Sales").
	WithHeight(300).
	Build()

// Send message with block
client.SendMessageWithOptions("user-id", aichars.SendMessageOptions{
	Block: cardBlock,
})

Available Block Types: Text, Divider, Section, Image, List, Link, Button, Card, Grid, Stat, Status, InfoBox, KeyValueList, Progress, CircularProgress, Timeline, Timer, Chart, Sparkline

See the blocks package for complete documentation.

API Reference

Client Methods

Method Description
NewClient(config Config) *Client Create a new WebSocket client
Connect() error Establish WebSocket connection
Disconnect() error Close connection gracefully
IsConnected() bool Check connection status
SendMessage(userID, content, type) (*Message, error) Send message to user
SendMessageWithOptions(userID, options) (*Message, error) Send message with options
ReplyToMessage(msg, content, type) (*Message, error) Reply to received message
ReplyToMessageWithOptions(msg, options) (*Message, error) Reply with options
ReplyToMessageWithBlock(msg, block) (*Message, error) Reply with block
SendCharMessage(chatID, content, type) (*Message, error) Send to existing chat
SendCharMessageWithOptions(chatID, options) (*Message, error) Send to chat with options
RequestWallet(userID, options) (*Message, error) Request wallet address
RequestPayment(userID, options) (*Message, error) Request payment
SendPayment(userID, options) (*Message, error) Send payment
CreatePost(options) (*Post, error) Create post
UpdateSpotlight(block) error Update spotlight card
UpdateSpotlightWithOptions(options) error Update spotlight with options
UploadImage(data, contentType) (string, error) Upload image, returns CDN URL
AckMessage(messageID) error Acknowledge message receipt

Features

  • ✅ WebSocket connection with auto-reconnect
  • ✅ Message handling with callbacks
  • ✅ Send/reply messages
  • ✅ Rich blocks system
  • ✅ Wallet & payment requests
  • ✅ Post creation
  • ✅ Spotlight management
  • ✅ Image upload
  • ✅ Message acknowledgment
  • ✅ Thread-safe operations

License

See LICENSE file in the repository root.

About

AIChars SDK for Go language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages