userclient

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: GPL-3.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExtenApis = map[string]ExtenApiFunc{
	"SetProfile": ExtenSetProfile,
}
View Source
var ExtenSetProfile = func(ctx context.Context, ectx *ext.Context, input map[string]any) (map[string]any, error) {
	firstName, _ := input["first_name"].(string)
	lastName, _ := input["last_name"].(string)
	avatarUrl, _ := input["avatar_url"].(string)
	bio, _ := input["bio"].(string)
	username, _ := input["username"].(string)
	if firstName == "" && lastName == "" && avatarUrl == "" && bio == "" && username == "" {
		return nil, errors.New("no profile data provided")
	}
	raw := ectx.Raw
	var errs []error
	if firstName != "" || lastName != "" {
		req := &tg.AccountUpdateProfileRequest{}
		req.SetFirstName(firstName)
		req.SetLastName(lastName)
		if bio != "" {
			req.SetAbout(bio)
		}
		_, err := raw.AccountUpdateProfile(ctx, req)
		if err != nil {
			errs = append(errs, fmt.Errorf("failed to update profile: %w", err))
		}
	} else if bio != "" {
		req := &tg.AccountUpdateProfileRequest{}
		req.SetAbout(bio)
		_, err := raw.AccountUpdateProfile(ctx, req)
		if err != nil {
			errs = append(errs, fmt.Errorf("failed to update bio: %w", err))
		}
	}
	if avatarUrl != "" {
		err := func() error {
			deleteOldErr := func() error {
				req := &tg.PhotosGetUserPhotosRequest{
					UserID: ectx.Self.AsInput(),
					Limit:  1,
					Offset: 1,
				}
				photos, err := raw.PhotosGetUserPhotos(ctx, req)
				if err != nil {
					return fmt.Errorf("failed to get user photos: %w", err)
				}
				if len(photos.GetPhotos()) == 0 {
					return nil
				}
				photo := photos.GetPhotos()[0]
				if photo == nil {
					return nil
				}
				photoInput, ok := photo.AsNotEmpty()
				if !ok {
					return fmt.Errorf("unexpected empty photo in response")
				}
				_, deleteOldErr := raw.PhotosDeletePhotos(ctx, []tg.InputPhotoClass{photoInput.AsInput()})
				if deleteOldErr != nil {
					return fmt.Errorf("failed to delete old profile photo: %w", deleteOldErr)
				}
				return nil
			}()
			if deleteOldErr != nil {
				log.FromContext(ctx).Warn("failed to delete old profile photo", "error", deleteOldErr)
			}
			var file tg.InputFileClass
			var err error
			if strings.HasPrefix(avatarUrl, "http") {
				file, err = uploader.NewUploader(raw).FromURL(ctx, avatarUrl)
			} else {

				base64Data := avatarUrl
				if idx := strings.Index(base64Data, ","); idx != -1 {
					base64Data = base64Data[idx+1:]
				}
				data, err2 := base64.StdEncoding.DecodeString(base64Data)
				if err2 != nil {
					return fmt.Errorf("failed to decode base64 avatar: %w", err)
				}
				file, err = uploader.NewUploader(raw).FromBytes(ctx, fmt.Sprintf("%s.jpg", xid.New().String()), data)
			}
			if err != nil {
				return fmt.Errorf("failed to upload avatar: %w", err)
			}
			req := &tg.PhotosUploadProfilePhotoRequest{}
			req.SetFile(file)
			photosPhoto, err := raw.PhotosUploadProfilePhoto(ctx, req)
			if err != nil {
				return fmt.Errorf("failed to set profile photo: %w", err)
			}
			photo, ok := photosPhoto.GetPhotoAsNotEmpty()
			if !ok {
				return fmt.Errorf("unexpected empty photo in response")
			}
			_, err = raw.PhotosUpdateProfilePhoto(ctx, &tg.PhotosUpdateProfilePhotoRequest{
				ID: photo.AsInput(),
			})
			if err != nil {
				return fmt.Errorf("failed to update profile photo: %w", err)
			}
			return nil
		}()
		if err != nil {
			errs = append(errs, fmt.Errorf("failed to update avatar: %w", err))
		}
	}
	if username != "" {
		_, err := raw.AccountUpdateUsername(ctx, username)
		if err != nil {
			errs = append(errs, fmt.Errorf("failed to update username: %w", err))
		}
	}
	if len(errs) > 0 {
		return nil, multierr.Combine(errs...)
	}
	return map[string]any{
		"message": "Profile updated successfully",
	}, nil
}

ExtenSetProfile updates the user's profile information.

Input:

- first_name: First name of the user.

- last_name: Last name of the user.

- avatar_url: URL of the avatar image (can be base64 encoded).

- bio: Bio of the user.

Functions

func DeleteHandler

func DeleteHandler(ctx *ext.Context, u *ext.Update) error

func WatchHandler

func WatchHandler(ctx *ext.Context, u *ext.Update) error

Types

type ExtenApiFunc added in v0.14.0

type ExtenApiFunc func(ctx context.Context, ectx *ext.Context, input map[string]any) (map[string]any, error)

type UserClient

type UserClient struct {
	TClient *gotgproto.Client

	GlobalIgnoreUsers []int64
	// contains filtered or unexported fields
}

func GetUserClient added in v0.10.0

func GetUserClient() *UserClient

func NewUserClient

func NewUserClient(ctx context.Context) (*UserClient, error)

func (*UserClient) AddGlobalIgnoreUser

func (u *UserClient) AddGlobalIgnoreUser(userID int64)

func (*UserClient) CallExtenApi added in v0.14.0

func (u *UserClient) CallExtenApi(ctx context.Context, name string, input map[string]any) (map[string]any, error)

func (*UserClient) Close

func (u *UserClient) Close() error

func (*UserClient) ForwardMessages added in v0.11.0

func (u *UserClient) ForwardMessages(ctx context.Context, fromChatID, toChatID int64, messageID []int) error

func (*UserClient) ForwardMessagesToFav added in v0.4.0

func (u *UserClient) ForwardMessagesToFav(ctx context.Context, fromID int64, messageIDs []int) error

func (*UserClient) GetContext added in v0.12.0

func (u *UserClient) GetContext() *ext.Context

func (*UserClient) RemoveGlobalIgnoreUser

func (u *UserClient) RemoveGlobalIgnoreUser(userID int64)

func (*UserClient) ReplyMessage added in v0.10.0

func (u *UserClient) ReplyMessage(ctx context.Context, chatID int64, messageID int, text string) (*types.Message, error)

func (*UserClient) StartWatch

func (u *UserClient) StartWatch(ctx context.Context)

func (*UserClient) SyncPeers added in v0.13.0

func (u *UserClient) SyncPeers(ctx context.Context) error

Jump to

Keyboard shortcuts

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