Go is a good language for building chatbots and conversational agents, especially the server side: receiving webhooks from Slack, Telegram or WhatsApp, managing conversation state, calling NLP or large language model APIs, and handling many users concurrently. Its goroutines, static binaries and standard HTTP stack make bots fast to run and easy to deploy.
This article shows how to structure a Go chatbot, with a working webhook, a simple intent router and a context-aware call to an external model API, plus notes on state, security and testing.
net/http, encoding/json and context cover webhooks, API calls and timeouts without extra dependencies.Python has more machine learning libraries, so many teams train or host models there and keep the bot's orchestration layer in Go.
A typical bot has four parts:
| Part | Responsibility |
|---|---|
| Channel adapter | Receives messages from Slack, Telegram, a web chat or voice and sends replies |
| Dialogue manager | Keeps conversation state and decides what to do next |
| Understanding | Rules, intent classification or an LLM that interprets the message |
| Integrations | Databases, order systems, search and other tools the bot calls |
Keeping these separate lets you swap a rule-based engine for an LLM without touching the channel code.
This example accepts JSON messages, matches simple intents and replies. It uses only the standard library. Note that io/ioutil is deprecated, so the code uses json.NewDecoder directly.
Test it with curl -X POST localhost:8080/webhook -d '{"user_id":"1","text":"Hi there"}'. Keyword matching is crude, for example "hi" matches inside "this", so use word boundaries or a classifier for anything beyond a demo.
You have three realistic options:
Whichever you choose, call it through an interface so you can test the bot with a fake.
Wrap every external call with context.WithTimeout, retry only idempotent failures with backoff, and cap the length of history you send. Use the provider's official Go SDK where one exists, since request formats change.
Bots need memory of earlier turns. Store state per user or conversation ID.
Slack, Telegram, Discord, Microsoft Teams and WhatsApp each provide an API and usually a webhook or socket mode. Use a maintained Go client for the platform, or call the HTTP API directly. Always verify request signatures, for example Slack's signing secret, before trusting a webhook, and keep tokens in environment variables or a secret manager.
httptest to test webhooks without a network.log/slog, and avoid logging personal data.Yes for the application layer: concurrency, HTTP and deployment are strong. For training or running models locally, Python has more tooling, so Go usually calls a model through an API.
Use a currently maintained client for the platform, such as slack-go/slack for Slack, and check recent releases. Calling the HTTP API directly is also reasonable for simple bots.
Store recent turns per conversation ID in Redis or a database and send a bounded history with each request.
Verify webhook signatures, rate limit by user, cap input size and never pass untrusted text into commands or queries without validation.
Build chatbots in Go by separating the channel adapter, dialogue manager, language understanding and integrations. Start with a webhook and intent routing, put language understanding behind an interface, store state in Redis or a database, and protect every external call with timeouts and signature checks.