An open API service indexing awesome lists of open source software.

https://github.com/robertgzr/joe-telegram-adapter

Telegram adapter for the Joe bot library
https://github.com/robertgzr/joe-telegram-adapter

bot chat joe telegram

Last synced: over 1 year ago
JSON representation

Telegram adapter for the Joe bot library

Awesome Lists containing this project

README

          

Joe Bot - Telegram Adapter


Connecting joe with the Telegram chat application. https://github.com/go-joe/joe





---

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

## Getting Started

This library is packaged using [Go modules][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:

```go
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](https://core.telegram.org/bots#3-how-do-i-create-a-bot).

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

- `joe.ReceiveMessageEvent`
- `ReceiveCommandEvent`

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

```go
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][tgbotapi] is *nice to have* when writing
bots:

```go
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](LICENSE)

[joe]: https://github.com/go-joe/joe
[tgbotapi]: https://github.com/go-telegram-bot-api/telegram-bot-api
[go-modules]: https://github.com/golang/go/wiki/Modules