telebot

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2016 License: MIT Imports: 11 Imported by: 0

README

Telebot

A simple Telegram bot skeleton written in Go.

Usage

You simply need a configuration (BotName + API Key) and a Response function.

Checkout this sample code:

package main

import "github.com/cortinico/telebot"

func main() {
	conf := telebot.Configuration{
		BotName: "SampleBot",
		ApiKey:  "162227600:AAAAAAAAAAABBBBBBBBBBCCCCCCCCCDDDDD"}

	var bot telebot.Bot

	bot.Start(conf, func(mess string) (string, error) {
		var answer string
		switch mess {
		case "/test":
			answer = "Test command works :)"
		default:
			answer = "You typed " + mess
		}
		return answer, nil
	})
}

Licence

The following software is released under the MIT Licence

Documentation

Overview

Package for creating a running a simple Telegram bot. This bot is capable just to answer simple user/group messages, all the logic must be implemented inside a Responder func

Package for creating a running a simple Telegram bot. This bot is capable just to answer simple user/group messages, all the logic must be implemented inside a Responder func

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

type Bot struct{}

Struct representing a telegram Bot (will implement IBot). Bot has no field (no state), it's just an empty bot

func (Bot) LoadSettings

func (t Bot) LoadSettings(filename string) (Configuration, error)

Load a configuration from a Json file and returns a configuration. See file `settings.json.sample` to see how settings should be formatted.

func (Bot) Start

func (t Bot) Start(conf Configuration, resp Responder)

Starts the telegram bot. The parameter conf represent the running configuration. The conf is mandatory otherwise the bot can't authenticate. The parameter resp is the Responder function. Also this parameter is mandatory, otherwise the bot don't know how to anser to user questions.

type Configuration

type Configuration struct {
	BotName string `json:"BotName"` // Name of the bot
	ApiKey  string `json:"ApiKey"`  // API Key of the bot (ask @BotFather)
	Timeout string `json:"Timeout"` // Timeout in seconds for polling
}

Configuration struct representing the configuration used from the bot to run properly. Configuration is usually loaded from file, or hardcoded inside the client code.

type IBot

type IBot interface {
	Start(conf Configuration, resp Responder)
	LoadSettings(filename string) (Configuration, error)
	// contains filtered or unexported methods
}

Interface Repserent a generic telegram bot. Exported functions are just LoadSettings to load a configuration and Start to launch the bot.

type Responder

type Responder func(string) (string, error)

Responder function, responsible of handling to user commands. This function represent the logic of your bot, you must provide a couple (string, error) for every message. The returned string will be sent to the user. If you set the error, the user will see an informative message.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL