gotelebot

package module
v0.0.0-...-82299e7 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2016 License: GPL-2.0 Imports: 14 Imported by: 2

README

gotelebot

GoDoc

Golang (Go) implementation for the Telegram Bot API.

This project provide a wrapper around the Telegram Bot API with golang. You can easy to ues telegram bot api in golang way by use this project. And gotelebot provided polling method let developer easy to get new messages.

Almost all method in Telegram bot api have been implement. While official telegram bot api update, this project will update as soon as possible.

Installation

go get github.com/eternnoir/gotelebot

Document

Full godoc document http://godoc.org/github.com/eternnoir/gotelebot

Echo Bot Example
package main

import (
	"fmt"
	"github.com/eternnoir/gotelebot"
)

func main() {
	// Echo Bot example.

	// Create gotelebot instance
	bot := gotelebot.InitTeleBot("TOKEN")
	// Start get new message whit goroutine and 60s timeout.
	go bot.StartPolling(true, 60)
	go processNewMessage(bot)
	processNewInlineQuery(bot)
}

func processNewMessage(bot *gotelebot.TeleBot) {
	newMsgChan := bot.Messages
	for {
		m := <-newMsgChan // Get new messaage, when new message arrive.
		fmt.Printf("Get Message:%#v \n", m)
		if m.Text != "" { // Check message is text message.
			bot.SendMessage(int(m.Chat.Id), m.Text, nil)
		}
	}
}

func processNewInlineQuery(bot *gotelebot.TeleBot) {
	newQuery := bot.InlineQuerys
	for {
		q := <-newQuery
		fmt.Printf("Get NewInlineQuery:%#v \n", q)
		if q.Query != "" {	// Only return result when query string not empty.
			result1 := types.NewInlineQueryResultArticl()
			result1.Id = "1"
			result1.Title = "Example"
			result1.MessageText = "Hi" + q.Query
			_, err := bot.AnswerInlineQuery(q.Id, []interface{}{result1}, nil)
			if err != nil {
				fmt.Println(err)
			}
		}
	}
}


Telegram Bot API Support

Methods

Telegram Bot API Method gotelebot Method Status
getMe GetMe Supported
sendMessage SendMessage Supported
forwardMessage ForwardMessage Supported
sendPhoto SendPhoto Supported
sendAudio SendAudio Supported
sendDocument SendDocument Supported
sendSticker SendSticker Supported
sendVideo SendVideo Supported
sendVoice SendVoice Supported
sendLocation SendLocation Supported
sendChatAction SendChatAction Supported
getUserProfilePhotos GetUserProfilePhotos Supported
getUpdates GetUpdates Supported
getFile GetFile Supported
inline mode inline mode Supported

Change Log

2015-10-12

Documentation

Overview

Golang (Go) implementation for the Telegram Bot API.

Example
package main

import (
	"github.com/eternnoir/gotelebot"
)

func main() {
	// Echo Bot example.
	bot := gotelebot.InitTeleBot("TOKEN") // Create gotelebot instance
	go bot.StartPolling(true, 0)          // Start get new message whit goroutine and default timeout.
	newMsgChan := bot.Messages
	for {
		m := <-newMsgChan // Get new messaage, when new message arrive.
		if m.Text != "" { // Check message is text message.
			bot.SendMessage(int(m.Chat.Id), m.Text, nil)
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnswerInlineQueryOptional

type AnswerInlineQueryOptional struct {
	CacheTime  *int
	IsPersonal *bool
	NextOffset *string
}

func (*AnswerInlineQueryOptional) AppendPayload

func (opt *AnswerInlineQueryOptional) AppendPayload(payload *url.Values)

type GetUserProfilePhotosOptional

type GetUserProfilePhotosOptional struct {
	Offset *int
	Limit  *int
}

func (*GetUserProfilePhotosOptional) AppendPayload

func (opt *GetUserProfilePhotosOptional) AppendPayload(payload *url.Values)

type Optional

type Optional interface {
	AppendPayload(payload *url.Values)
}

type SendAudioOptional

type SendAudioOptional struct {
	// Duration of the audio in seconds
	Duration         *int
	Performer        *string
	Title            *string
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendAudio method

func (*SendAudioOptional) AppendPayload

func (opt *SendAudioOptional) AppendPayload(payload *url.Values)

type SendDocumentOptional

type SendDocumentOptional struct {
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendDocument method

func (*SendDocumentOptional) AppendPayload

func (opt *SendDocumentOptional) AppendPayload(payload *url.Values)

type SendLocationOptional

type SendLocationOptional struct {
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendLocation method

func (*SendLocationOptional) AppendPayload

func (opt *SendLocationOptional) AppendPayload(payload *url.Values)

type SendMessageOptional

type SendMessageOptional struct {
	// Send Markdown, if you want Telegram apps to show bold, italic and inline URLs in your bot's message.
	ParseMode *string
	// Disables link previews for links in this message
	DisableWebPagePreview *bool
	ReplyToMessageId      *int
	ReplyMarkup           *types.ReplyMarkup
}

Optional parameters for SendMessage method

func (*SendMessageOptional) AppendPayload

func (opt *SendMessageOptional) AppendPayload(payload *url.Values)

type SendPhotoOptional

type SendPhotoOptional struct {
	// Photo caption
	Caption          *string
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendPhoto method

func (*SendPhotoOptional) AppendPayload

func (opt *SendPhotoOptional) AppendPayload(payload *url.Values)

type SendStickerOptional

type SendStickerOptional struct {
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendSticker method

func (*SendStickerOptional) AppendPayload

func (opt *SendStickerOptional) AppendPayload(payload *url.Values)

type SendVideoOptional

type SendVideoOptional struct {
	Duration         *int
	Caption          *string
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendVideo method

func (*SendVideoOptional) AppendPayload

func (opt *SendVideoOptional) AppendPayload(payload *url.Values)

type SendVoiceOptional

type SendVoiceOptional struct {
	Duration         *int
	ReplyToMessageId *int
	ReplyMarkup      *types.ReplyMarkup
}

Optional parameters for SendVoice method

func (*SendVoiceOptional) AppendPayload

func (opt *SendVoiceOptional) AppendPayload(payload *url.Values)

type TeleBot

type TeleBot struct {
	Messages            chan (*types.Message)
	InlineQuerys        chan (*types.InlineQuery)
	ChosenInlineResults chan (*types.ChosenInlineResult)
	Offset              float64
	// contains filtered or unexported fields
}

func InitTeleBot

func InitTeleBot(botToken string) *TeleBot

InitTeleBot is the function to create gotelebot instance.

func (*TeleBot) AnswerInlineQuery

func (bot *TeleBot) AnswerInlineQuery(inlineQueryId string, results []interface{}, opt *AnswerInlineQueryOptional) (bool, error)

func (*TeleBot) DownloadFile

func (bot *TeleBot) DownloadFile(fileId string) (*[]byte, error)

Download file by fileId.

Example
package main

import (
	"fmt"
	"io/ioutil"

	"github.com/eternnoir/gotelebot"
)

func main() {
	token := "TOKEN"
	bot := gotelebot.InitTeleBot(token)
	fi := "BQADBQADnAMAAsYifgZph-iT9_z_rgI"
	file, err := bot.DownloadFile(fi)
	if err != nil {
		fmt.Println("Bot get File error")
		return
	}
	ferr := ioutil.WriteFile("/tmp/data", *file, 0644)
	if ferr != nil {
		fmt.Println("Write to File error")
	}
}
Output:

func (*TeleBot) ForwardMessage

func (bot *TeleBot) ForwardMessage(chatid, from_chat_id, message_id int) (*types.Message, error)

Use this method to forward messages of any kind. On success, the sent Message type is returned.

func (*TeleBot) GetFile

func (bot *TeleBot) GetFile(fileId string) (*types.File, error)

Use this method to get basic info about a file and prepare it for downloading.

func (*TeleBot) GetMe

func (bot *TeleBot) GetMe() (*types.User, error)

Returns basic information about the bot in form of a User object.

Example
package main

import (
	"fmt"

	"github.com/eternnoir/gotelebot"
)

func main() {
	bot := gotelebot.InitTeleBot("TOKEN") // Create gotelebot instance
	me, err := bot.GetMe()                // Get user object.
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(me.FirstName)
}
Output:

func (*TeleBot) GetUpdates

func (bot *TeleBot) GetUpdates(offset, limit string, timeout int) ([]*types.Update, error)

Use this method to receive incoming updates using long polling. An Array of Update objects is returned.

func (*TeleBot) GetUserProfilePhotos

func (bot *TeleBot) GetUserProfilePhotos(userid int, opt *GetUserProfilePhotosOptional) (*types.UserProfilePhotos, error)

Use this method to get a list of profile pictures for a user.

func (*TeleBot) SendAudio

func (bot *TeleBot) SendAudio(chatid int, audio string, opt *SendAudioOptional) (*types.Message, error)

Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent Message is returned.

Use SendAudioOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

func (*TeleBot) SendChatAction

func (bot *TeleBot) SendChatAction(chatid int, action string) (string, error)

Use this method when you need to tell the user that something is happening on the bot's side.

action can be : "typing" for text messages, "upload_photo" for photos, "record_video" or upload_video for videos, "record_audio" or upload_audio for audio files, "upload_document" for general files, "find_location" for location data.

func (*TeleBot) SendDocument

func (bot *TeleBot) SendDocument(chatid int, document string, opt *SendDocumentOptional) (*types.Message, error)

Use this method to send general files. On success, the sent Message is returned.

Use SendDocumentOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

func (*TeleBot) SendLocation

func (bot *TeleBot) SendLocation(chatid int, latitude, longitude float64, opt *SendLocationOptional) (*types.Message, error)

Use this method to send point on the map. On success, the sent Message is returned.

Use SendLocationOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

func (*TeleBot) SendMessage

func (bot *TeleBot) SendMessage(chatid int, text string, opt *SendMessageOptional) (*types.Message, error)

Use this method to send text messages. On success, the sent Message type is returned.

Use SendMessageOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

Example
package main

import (
	"fmt"

	"github.com/eternnoir/gotelebot"
)

func main() {
	bot := gotelebot.InitTeleBot("TOKEN") // Create gotelebot instance
	testMsg := "Test Msg"
	chatid := 11111111
	_, err := bot.SendMessage(chatid, testMsg, nil)
	if err != nil {
		fmt.Println("Bot send message error")
	}
}
Output:

func (*TeleBot) SendPhoto

func (bot *TeleBot) SendPhoto(chatid int, photo string, opt *SendPhotoOptional) (*types.Message, error)

Use this method to send photos. On success, the sent Message is returned.

Use SendPhotoOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

Example
package main

import (
	"fmt"

	"github.com/eternnoir/gotelebot"
)

func main() {
	bot := gotelebot.InitTeleBot("TOKEN") // Create gotelebot instance
	chatid := 11111111
	filePath := "./test_data/go.png"
	_, err := bot.SendPhoto(chatid, filePath, nil)
	if err != nil {
		fmt.Println("Bot send Photo error")
	}
}
Output:

func (*TeleBot) SendSticker

func (bot *TeleBot) SendSticker(chatid int, sticker string, opt *SendStickerOptional) (*types.Message, error)

Use this method to send .webp stickers. On success, the sent Message is returned.

Use SendStickerOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

func (*TeleBot) SendVideo

func (bot *TeleBot) SendVideo(chatid int, video string, opt *SendVideoOptional) (*types.Message, error)

Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document).

Use SendVideoOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

func (*TeleBot) SendVoice

func (bot *TeleBot) SendVoice(chatid int, voice string, opt *SendVoiceOptional) (*types.Message, error)

Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned.

Use SendVideoOptional to setup optional parameters. If you don't want use any optional parameters, just asign nil to opt.

func (*TeleBot) StartPolling

func (bot *TeleBot) StartPolling(nonStop bool, timeout int) error

Let gotelebot always try to get new messages. This function will put new message to gotelebot's Message channel.

func (*TeleBot) StopPolling

func (bot *TeleBot) StopPolling()

Let gotelebot stop polling new messages.

Directories

Path Synopsis
This package define all inline type used in Telegram Bot API.
This package define all inline type used in Telegram Bot API.

Jump to

Keyboard shortcuts

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