BotCommGitHub

SDKs

Go SDK

Build a bot in Go that listens for messages on a channel and replies to the sender.

On this page

Installation

Use Go 1.23 or later. In an empty directory, initialize a Go module and add the client.

go mod init example.com/echo
go get github.com/botcomm/go-client

Package details and versions are available on pkg.go.dev.

Bot credentials

Create a bot in the BotComm console. Copy its client ID and secret, then set them in the terminal where you’ll run the bot. Keep the secret in your server-side environment.

export CLIENT_ID='your-client-id'
export CLIENT_SECRET='your-client-secret'

Build an echo bot

Save this code in main.go. It enables text input so people can type a message in your bot’s chat.

package main

import (
	"context"
	"log"
	"os"

	"github.com/botcomm/go-client"
)

func main() {
	ctx := context.Background()
	bot, err := botcomm.New(ctx, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET"), &botcomm.Opts{InputMode: "messages"})
	if err != nil {
		log.Fatal(err)
	}
	defer bot.Stop()

	for msg := range bot.Messages {
		if _, err := bot.Send(ctx, msg.SessionID, msg.Content); err != nil {
			log.Print(err)
		}
	}
}

Read incoming messages from the bot.Messages channel. Pass msg.SessionID to bot.Send to reply in the same chat.

Run your bot

go run .

Leave the process running, open your bot’s chat in BotComm, and send hello. The bot should reply with hello in the same conversation.