query

package
v0.112.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 7 Imported by: 15

Documentation

Overview

Package query contains generic pagination helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBlocked added in v0.36.0

func GetBlocked(raw *tg.Client) *blocked.GetBlockedQueryBuilder

GetBlocked creates blocked.GetBlockedQueryBuilder. Shorthand for

query.NewQuery(raw).GetBlocked()

func GetDialogs added in v0.36.0

func GetDialogs(raw *tg.Client) *dialogs.GetDialogsQueryBuilder

GetDialogs creates dialogs.GetDialogsQueryBuilder. Shorthand for

query.NewQuery(raw).GetDialogs()

func GetOldFeaturedStickers added in v0.36.0

func GetOldFeaturedStickers(raw *tg.Client) *featured.GetOldFeaturedStickersQueryBuilder

GetOldFeaturedStickers creates featured.QueryBuilder. Shorthand for

query.NewQuery(raw).GetOldFeaturedStickers()

func GetParticipants added in v0.36.0

func GetParticipants(raw *tg.Client, channel tg.InputChannelClass) *participants.GetParticipantsQueryBuilder

GetParticipants creates participants.GetParticipantsQueryBuilder. Shorthand for

query.NewQuery(raw).GetParticipants(channel)

func GetUserPhotos added in v0.36.0

func GetUserPhotos(raw *tg.Client, user tg.InputUserClass) *photos.GetUserPhotosQueryBuilder

GetUserPhotos creates photos.GetUserPhotosQueryBuilder. Shorthand for

query.NewQuery(raw).GetUserPhotos(user)

func Messages added in v0.36.0

func Messages(raw *tg.Client) *messages.QueryBuilder

Messages creates messages.QueryBuilder. Shorthand for

query.NewQuery(raw).Messages()

Types

type Query

type Query struct {
	// contains filtered or unexported fields
}

Query is common struct to create query builders.

Example (DownloadSaved)
package main

import (
	"context"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/downloader"
	"github.com/gotd/td/telegram/query"
	"github.com/gotd/td/telegram/query/messages"
	"github.com/gotd/td/tg"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		panic(err)
	}

	// This example downloads all attachments (photo, video, docs, etc.)
	// from SavedMessages dialog.
	if err := client.Run(ctx, func(ctx context.Context) error {
		raw := tg.NewClient(client)
		d := downloader.NewDownloader()
		return query.Messages(raw).GetHistory(&tg.InputPeerSelf{}).ForEach(ctx,
			func(ctx context.Context, elem messages.Elem) error {
				f, ok := elem.File()
				if !ok {
					return nil
				}

				_, err := d.Download(raw, f.Location).ToPath(ctx, f.Name)
				return err
			})
	}); err != nil {
		panic(err)
	}
}
Output:

Example (GetAdmins)
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/query"
	"github.com/gotd/td/telegram/query/channels/participants"
	"github.com/gotd/td/telegram/query/dialogs"
	"github.com/gotd/td/tg"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		panic(err)
	}

	// This example iterates over all channels and prints admins.
	if err := client.Run(ctx, func(ctx context.Context) error {
		raw := tg.NewClient(client)
		cb := func(ctx context.Context, dlg dialogs.Elem) error {
			// Skip deleted dialogs.
			if dlg.Deleted() {
				return nil
			}

			q, ok := dlg.Participants(raw)
			if !ok {
				return nil
			}

			return q.ForEach(ctx, func(ctx context.Context, elem participants.Elem) error {
				user, admin, ok := elem.Admin()
				if !ok {
					return nil
				}

				fmt.Println(user.Username, "admin")
				if admin.AdminRights.ChangeInfo {
					fmt.Println("\t+ ChangeInfo")
				}
				if admin.AdminRights.PostMessages {
					fmt.Println("\t+ PostMessages")
				}
				if admin.AdminRights.EditMessages {
					fmt.Println("\t+ EditMessages")
				}
				if admin.AdminRights.DeleteMessages {
					fmt.Println("\t+ DeleteMessages")
				}
				if admin.AdminRights.BanUsers {
					fmt.Println("\t+ BanUsers")
				}
				if admin.AdminRights.InviteUsers {
					fmt.Println("\t+ InviteUsers")
				}
				if admin.AdminRights.PinMessages {
					fmt.Println("\t+ PinMessages")
				}
				if admin.AdminRights.AddAdmins {
					fmt.Println("\t+ AddAdmins")
				}
				if admin.AdminRights.Anonymous {
					fmt.Println("\t+ Anonymous")
				}
				if admin.AdminRights.ManageCall {
					fmt.Println("\t+ ManageCall")
				}
				if admin.AdminRights.Other {
					fmt.Println("\t+ Other")
				}
				return nil
			})
		}

		return query.GetDialogs(raw).ForEach(ctx, cb)
	}); err != nil {
		panic(err)
	}
}
Output:

Example (IterAllMessages)
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/query"
	"github.com/gotd/td/telegram/query/dialogs"
	"github.com/gotd/td/telegram/query/messages"
	"github.com/gotd/td/tg"
)

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		panic(err)
	}

	// This example iterates over all messages of all dialogs of user and prints them.
	if err := client.Run(ctx, func(ctx context.Context) error {
		raw := tg.NewClient(client)
		cb := func(ctx context.Context, dlg dialogs.Elem) error {
			// Skip deleted dialogs.
			if dlg.Deleted() {
				return nil
			}

			return dlg.Messages(raw).ForEach(ctx, func(ctx context.Context, elem messages.Elem) error {
				msg, ok := elem.Msg.(*tg.Message)
				if !ok {
					return nil
				}
				fmt.Println(msg.Message)

				return nil
			})
		}

		return query.GetDialogs(raw).ForEach(ctx, cb)
	}); err != nil {
		panic(err)
	}
}
Output:

func NewQuery

func NewQuery(raw *tg.Client) *Query

NewQuery creates Query.

func (*Query) Blocked

func (q *Query) Blocked() *blocked.QueryBuilder

Blocked creates blocked.QueryBuilder

func (*Query) Dialogs

func (q *Query) Dialogs() *dialogs.QueryBuilder

Dialogs creates dialogs.QueryBuilder

func (*Query) Featured

func (q *Query) Featured() *featured.QueryBuilder

Featured creates featured.QueryBuilder

func (*Query) GetBlocked added in v0.36.0

func (q *Query) GetBlocked() *blocked.GetBlockedQueryBuilder

GetBlocked creates blocked.GetBlockedQueryBuilder.

func (*Query) GetDialogs added in v0.36.0

func (q *Query) GetDialogs() *dialogs.GetDialogsQueryBuilder

GetDialogs creates dialogs.GetDialogsQueryBuilder.

func (*Query) GetOldFeaturedStickers added in v0.36.0

func (q *Query) GetOldFeaturedStickers() *featured.GetOldFeaturedStickersQueryBuilder

GetOldFeaturedStickers creates featured.QueryBuilder.

func (*Query) GetParticipants added in v0.36.0

func (q *Query) GetParticipants(channel tg.InputChannelClass) *participants.GetParticipantsQueryBuilder

GetParticipants creates participants.GetParticipantsQueryBuilder.

func (*Query) GetUserPhotos added in v0.36.0

func (q *Query) GetUserPhotos(user tg.InputUserClass) *photos.GetUserPhotosQueryBuilder

GetUserPhotos creates photos.GetUserPhotosQueryBuilder.

func (*Query) Messages

func (q *Query) Messages() *messages.QueryBuilder

Messages creates messages.QueryBuilder.

func (*Query) Participants

func (q *Query) Participants() *participants.QueryBuilder

Participants creates participants.QueryBuilder

func (*Query) Photos

func (q *Query) Photos() *photos.QueryBuilder

Photos creates photos.QueryBuilder

Directories

Path Synopsis
Package cached contains cached query helpers.
Package cached contains cached query helpers.
channels
participants
Package participants contains channel participants iteration helper.
Package participants contains channel participants iteration helper.
contacts
blocked
Package blocked contains blocked contacts iteration helper.
Package blocked contains blocked contacts iteration helper.
Package dialogs contains dialog iteration helper.
Package dialogs contains dialog iteration helper.
Package hasher contains Telegram pagination hash implementation.
Package hasher contains Telegram pagination hash implementation.
internal
genutil
Package genutil is a utility package for query helpers codegeneration.
Package genutil is a utility package for query helpers codegeneration.
Package messages contains message iteration helper.
Package messages contains message iteration helper.
stickers/featured
Package featured contains featured stickers iteration helper.
Package featured contains featured stickers iteration helper.
Package photos contains photos iteration helper.
Package photos contains photos iteration helper.

Jump to

Keyboard shortcuts

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