bap

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 7 Imported by: 0

README

Bot Advertising Platform SDK

This repository holds SDK related to Bot Advertising Platform.

Requirements

Installation

To install the bap package, use the following command:

go get github.com/codd-tech/bap-sdk-go

Usage

Importing the Package

Import the bap-sdk-go package into your Go code:

import "github.com/codd-tech/bap-sdk-go"
Creating a BAP Client

To create a new instance of the BAP client, use the NewBAPClient function:

// Setup BAP
bapClient, err := bap.NewBAPClient("your-api-key")
if err != nil {
    log.Fatal(err)
}
defer bapClient.Close()

The APIKey field is mandatory and represents your Advertising Platform API key.

Handling Updates

Use the HandleUpdate method of the BAP client to send update data to the BAP API:

needHandle, err := bapClient.HandleUpdate(context.Background(), update)
if err != nil {
	log.Print(err)
}

If your advertisement mode is set to manual you can mark ad placement in your code by calling:

err = bapClient.SendAdvertisement(context.Background(), update)
if err != nil {
	log.Print(err)
}
Interrupting control flow

At times, BAP may introduce telegram updates within its advertisement flow. To maintain the logical consistency of your bot, it is necessary to ignore such updates.

The bap.HandleUpdate method returns a boolean value indicating whether you should proceed with handling the request or skip it as an internal BAP request.

When the method returns false, it signifies that the current request should not be processed by your bot.

Handling Updates Using Middleware (Example on Telebot)

telebot

Use the BapMiddleware

package main

import (
	"log"
	"time"

	"github.com/codd-tech/bap-sdk-go/middleware"

	tele "gopkg.in/telebot.v3"
)

func main() {
	pref := tele.Settings{
		Token:  "TELEGRAM_TOKEN",
		Poller: &tele.LongPoller{Timeout: 10 * time.Second},
	}

	b, err := tele.NewBot(pref)
	if err != nil {
		log.Fatal(err)
	}

	// Bap middleware
	b.Use(middleware.TelebotBapMiddleware("your-api-key"))

	b.Handle("/hello", func(c tele.Context) error {
		return c.Send("Hello Moderator!")
	})

	b.Start()
}

About

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub

License

Bot Advertising Platform SDK is licensed under the MIT License - see the LICENSE file for details

Documentation

Index

Constants

View Source
const (
	APIVersion              = 3
	CallbackQueryDataPrefix = "/__bap"
)

Variables

This section is empty.

Functions

func Validate added in v1.3.1

func Validate(update interface{}) (*tgbotapi.Update, error)

Validate the update received Update.

Types

type Client added in v1.3.1

type Client interface {
	// HandleUpdate sends the update data to the BAP API.
	//
	// Return false, if you do not need to handle this update (because it is a bap command).
	HandleUpdate(ctx context.Context, update interface{}) (bool, error)
	// SendAdvertisement send advertisement to the BAP API, allows you to immediately display ads
	SendAdvertisement(ctx context.Context, update interface{}) error
	// Close closes the BAP UDP connection.
	Close() error
}

Client is an interface that defines the methods for a BAP client.

func NewBAPClient

func NewBAPClient(apiKey string) (Client, error)

NewBAPClient Creates a new instance of the BAP client.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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