Skip to content

rogozhka/tgwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

232 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc Travis Go Report Card

TgWrap is a Go library with Telegram Bot API bindings. General approach is to keep objects as close as possible to the API structures with respect to optional params. Results of the methods are the same as noted in API documentation. Library does not pretend to be framework with tons of handlers and application strategy in mind. But there are some private helpers w/ reflection and struct tags under the hood. The aim is to cover step by step all the methods including Payment section.

Example

package main

import (
	"log"
	"fmt"
	"github.com/rogozhka/tgwrap"
)

func main() {

	bot := tgwrap.NewBot("<token>")

  	// returns *User
	me, err := bot.GetMe()
	if err != nil {
		panic(err)
	}

	log.Printf("BotID:%v", me.ID)
	log.Printf("BotName:%v", me.FirstName)

	updates, err := bot.GetUpdates(&tgwrap.GetUpdatesOpt{
		Timeout: 60,
	})
	if err != nil {
		panic(err)
	}

	for _, u := range updates {

		log.Printf("UpdateID:%v", u.ID)
		log.Printf("MessageID:%v", u.Message.ID)
		log.Printf("ChatID:%v", u.Message.Chat.ID)
		log.Printf("Text:%v", u.Message.Text)
		log.Printf("UserID:%v", u.Message.From.ID)
		log.Printf("FirstName:%v", u.Message.From.FirstName)

		// reply to message with "Re: <original message>"
		bot.SendMessage(u.Message.Chat.ID, fmt.Sprintf("Re: %v", u.Message.Text),
			&tgwrap.SendMessageOpt{
				DisableNotification: true,
			})

		// send photo from URL
		bot.SendPhoto(u.Message.Chat.ID,
             tgwrap.NewInputFileFromURL("http://imageurl.com/test.jpg"),
			&tgwrap.SendPhotoOpt{
				DisableNotification: true,
				Caption:             "Test gray photo",
			})

		bot.SendChatAction(u.Message.Chat.ID, tgwrap.ChatActionUploadPhoto)

		// send photo from fs
		bot.SendPhoto(u.Message.Chat.ID,
			tgwrap.NewInputFileLocal("/tmp/local-file-name.jpg"),
			&tgwrap.SendPhotoOpt{
				DisableNotification: true,
				Caption:             "Photo from webcam ;)",
			})

		bot.SendChatAction(u.Message.Chat.ID, tgwrap.ChatActionRecordAudio)

		// send audio from fs
		bot.SendAudio(u.Message.Chat.ID,
			tgwrap.NewInputFileLocal("/tmp/song.mp3"),
			&tgwrap.SendAudioOpt{
				DisableNotification: true,
				Title:               "Song name",
				Performer:           "The Family Band",
				Caption:             "Lastest record",
			})
	}
}

Currently supported methods

BotUpdatesInterface group of methods to get updates and setup hooks:

  • getUpdates

  • setWebhook

  • deleteWebhook

  • getWebhookInfo

BotMessagesInterface to send messages and chat actions:

BotChatInterface to interact with chat state and settings:

BotMediaInterface for sending media objects, encoding local files, by url, file_id:

Contribution

Welcome feedback Issues and PR.

Licence

Released under the MIT License.

About

Go library with Telegram Bot 🤖 API bindings. Simple optional params. No extra framework-like functions.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages