Home

Awesome

<h1 align="center">Joe Bot - Telegram Adapter</h1> <p align="center">Connecting joe with the Telegram chat application. https://github.com/go-joe/joe</p> <p align="center"> <a href="https://github.com/robertgzr/joe-telegram-adapter/releases"><img src="https://img.shields.io/github/tag/robertgzr/joe-telegram-adapter.svg?label=version&color=brightgreen"></a> <a href="https://godoc.org/github.com/robertgzr/joe-telegram-adapter"><img src="https://img.shields.io/badge/godoc-reference-blue.svg?color=blue"></a> </p>

This repository contains a module for the Joe Bot library. Built using telegram-bot-api.

Getting Started

This library is packaged using Go modules. You can get it via:

go get github.com/robertgzr/joe-telegram-adapter

Example usage

In order to connect your bot to telegram you can simply pass it as module when creating a new bot:

package main

import (
	"github.com/go-joe/joe"
	"github.com/robertgzr/joe-telegram-adapter"
)

func main() {
	b := joe.New("example-bot",
		telegram.Adapter(os.Getenv("TELEGRAM_BOT_TOKEN")),
		…
	)
	
	b.Respond("ping", Pong)

	err := b.Run()
	if err != nil {
		b.Logger.Fatal(err.Error())
	}
}

For how to create a telegram bot and connect to it, see here.

This adapter will emit the following events to the robot brain:

A common use-case is handling Telegram bot commands, /command. To make this easy a custom event type is emitted:

package main

import (
	"github.com/go-joe/joe"
	"github.com/robertgzr/joe-telegram-adapter"
)

type ExampleBot {
    *joe.Bot
}

func main() {
	b := &ExampleBot{
	    Bot: joe.New("example-bot",
		    telegram.Adapter(os.Getenv("TELEGRAM_BOT_TOKEN")),
		    …
	    ),
	}

	b.Brain.RegisterHandler(b.HandleTelegramCommands)
	b.Respond("ping", Pong)

	err := b.Run()
	if err != nil {
		b.Logger.Fatal(err.Error())
	}
}

func (b *ExampleBot) HandleTelegramCommands(ev telegram.ReceiveCommandEvent) error {
	switch ev.Arg0 {
	case "command":
		b.Say(ev.Channel(), "Hello, world!")
		return nil
	default:
		return errors.New("unknown command")
	}
}

Additional features

Some features available via the Bot API is nice to have when writing bots:

tg, ok := Bot.Adapter.(*telegram.TelegramAdapter)

tg.BotAPI allows full access to the Bot API interface.

This package also provides some abstractions for ease of use:

// photo/gif/sticker can be file, FileReader, or FileBytes which will upload a
// new instance; or a string which assumes a telegram fileID
tg.SendPhoto(msg.Channel, photo, "caption")
tg.SendGif(msg.Channel, gif, "caption")
tg.SendSticker(msg.Channel, sticker)

License

BSD-3-Clause