telebotgae

package module
v0.0.0-...-3e8127e Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2016 License: MIT Imports: 11 Imported by: 0

README

Telebot 4 Google App Engine Build Status GoDoc

A simple Telegram bot skeleton written in Go (GAE Capable)

This library is derived from telebot and allows you to deploy your bot on Google App Engine. With this you can simply deploy your bot code on AppEngine and forget about server management and scaling.

Setup

  • Create a new bot interacting with @BotFather on Telegram. Open a chat with @BotFather and start asking with /newbot. He will guide you through the creation of a new bot, and he will give you an API Key, say 162227600:AAAAAAAAAAABBBBBBBBBBCCCCCCCCCDDDDD.
  • Copy the example bot skeleton inside the bot_example folder of this repository.
git clone https://github.com/cortinico/telebotgae.git && cd telebotgae/bot_example
  • Edit the file hello.go adding your bot name (without @) and your API Key
func init() {
	conf := telebotgae.Configuration{
		BotName: "MyNewSampleBot",
		ApiKey:  "162227600:AAAAAAAAAAABBBBBBBBBBCCCCCCCCCDDDDD"}
  • Edit the file app.yaml adding your project-id from Google Cloud Dashboard
application: my-telegrambot-project-id
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app
  secure: always
  • Grab this library and build the project
goapp get github.com/cortinico/telebotgae && goapp build
  • Deploy your bot to App Engine. If it's the first time you deploy, you will be asked for Google authentication.
goapp deploy
  • Visit the following web page in a web browser:
https://api.telegram.org/bot[API_KEY]/setWebhook?url=https://[PROJECT-ID].appspot.com

So your URL should look like this:

https://api.telegram.org/bot162227600:AAAAAAAAAAABBBBBBBBBBCCCCCCCCCDDDDD/setWebhook?url=https://my-telegrambot-project-id.appspot.com

DON'T FORGET TO DO IT, AND DON'T MISPELL, OTHERWISE YOUR BOT WON'T WORK

If you see this message:

{"ok":true,"result":true,"description":"Webhook was set"}

browser

Then your bot is working :D Have fun with telebotgae

hello.go

Your bot code should look like this:

package hello

import (
	"github.com/cortinico/telebotgae"
	"net/http"
)

func init() {
	conf := telebotgae.Configuration{
		BotName: "MyNewSampleBot",
		ApiKey:  "162227600:AAAAAAAAAAABBBBBBBBBBCCCCCCCCCDDDDD"}

	var bot telebotgae.Bot

	bot.Startgae(conf, func(mess string, r *http.Request) (string, error) {
		return "You typed " + mess, nil
	})
}

You can use the second parameter of Startgae to implement the logic of your bot. The second parameter must be a function with the following type:

type Responder func(string, *http.Request) (string, error)

You will receive in input a string with the message from the user, and an http.Request. You can use the http.Request to get access to all the App Engine nice feature such as Datastore, Memcache, etc.

You have to provide a tuple made by the answer and the error. Set the error to nil if nothing unexpected has occurred.

Configuration

Configuration can also be loaded from a JSON file, using the LoadSettings(filename string) (Configuration, error) function.

Licence

The following software is released under the MIT Licence

Documentation

Overview

Package for creating a running a simple Telegram bot on Google App Engine. 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) Startgae

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

Initialize the telegram bot. The parameter conf represents 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)
}

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 {
	Startgae(conf Configuration, resp Responder)
	LoadSettings(filename string) (Configuration, error)
	// contains filtered or unexported methods
}

Interface Representing a generic telegram bot. Exported functions are just LoadSettings to load a configuration and Startgae to initialize the bot handlers.

type Responder

type Responder func(string, *http.Request) (string, error)

Responder function, responsible of handling 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. The http.Request parameter can be used to get access to an App Engine context (to use the MemCache or other GAE features)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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