Skip to content

Commit

Permalink
GoTGProto v1.0.0-beta07
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 3, 2022
1 parent 191c68c commit 79b5b8c
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 65 deletions.
8 changes: 4 additions & 4 deletions clientHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
Sender *message.Sender
)

const VERSION = "v1.0.0-beta06"
const VERSION = "v1.0.0-beta07"

type ClientHelper struct {
// Unique Telegram Application ID, get it from https://my.telegram.org/apps.
Expand Down Expand Up @@ -59,7 +59,7 @@ type ClientHelper struct {
}

// StartClient is the helper for gotd/td which creates client, runs it, prepares storage etc.
func StartClient(c ClientHelper) {
func StartClient(c *ClientHelper) {
var sessionStorage telegram.SessionStorage
if c.Session.GetName() == ":memory:" {
sessionStorage = &session.StorageMemory{}
Expand All @@ -81,7 +81,7 @@ func StartClient(c ClientHelper) {

}

func (ClientHelper) Run(f func(ctx context.Context, log *zap.Logger) error) context.Context {
func (*ClientHelper) Run(f func(ctx context.Context, log *zap.Logger) error) context.Context {
clog, err := zap.NewProduction()
if err != nil {
panic(err)
Expand All @@ -94,7 +94,7 @@ func (ClientHelper) Run(f func(ctx context.Context, log *zap.Logger) error) cont
return ctx
}

func (ch ClientHelper) CreateClient(ctx context.Context, opts telegram.Options,
func (ch *ClientHelper) CreateClient(ctx context.Context, opts telegram.Options,
setup func(ctx context.Context, Client *telegram.Client) error,
cb func(ctx context.Context, Client *telegram.Client) error,
) error {
Expand Down
3 changes: 2 additions & 1 deletion examples/authorizing-as-user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"

"github.com/anonyindian/gotgproto"
"github.com/anonyindian/gotgproto/dispatcher"
"github.com/anonyindian/gotgproto/sessionMaker"
Expand All @@ -12,7 +13,7 @@ import (
func main() {
// custom dispatcher handles all the updates
dp := dispatcher.MakeDispatcher()
gotgproto.StartClient(gotgproto.ClientHelper{
gotgproto.StartClient(&gotgproto.ClientHelper{
// Get AppID from https://my.telegram.org/apps
AppID: 1234567,
// Get ApiHash from https://my.telegram.org/apps
Expand Down
3 changes: 2 additions & 1 deletion examples/echo-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"

"github.com/anonyindian/gotgproto"
"github.com/anonyindian/gotgproto/dispatcher"
"github.com/anonyindian/gotgproto/dispatcher/handlers"
Expand All @@ -16,7 +17,7 @@ import (
func main() {
// custom dispatcher handles all the updates
dp := dispatcher.MakeDispatcher()
gotgproto.StartClient(gotgproto.ClientHelper{
gotgproto.StartClient(&gotgproto.ClientHelper{
// Get AppID from https://my.telegram.org/apps
AppID: 1234567,
// Get ApiHash from https://my.telegram.org/apps
Expand Down
52 changes: 48 additions & 4 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ext

import (
"context"
"errors"
"strings"
"time"

Expand Down Expand Up @@ -116,6 +117,37 @@ func (ctx *Context) SendMedia(chatId int64, request *tg.MessagesSendMediaRequest
return functions.ReturnNewMessageWithError(m, u, err)
}

// SetInlineBotResult invokes method messages.setInlineBotResults#eb5ea206 returning error if any.
// Answer an inline query, for bots only
func (ctx *Context) SetInlineBotResult(request *tg.MessagesSetInlineBotResultsRequest) (bool, error) {
return ctx.Client.MessagesSetInlineBotResults(ctx, request)
}

func (ctx *Context) GetInlineBotResults(chatId int64, botUsername string, request *tg.MessagesGetInlineBotResultsRequest) (*tg.MessagesBotResults, error) {
bot := storage.GetPeerByUsername(botUsername)
if bot.ID == 0 {
c, err := ctx.ResolveUsername(botUsername)
if err != nil {
return nil, err
}
switch {
case c.IsAUser():
bot = &storage.Peer{
ID: c.GetID(),
AccessHash: c.GetAccessHash(),
}
default:
return nil, errors.New("provided username was invalid for a bot")
}
}
request.Peer = storage.GetInputPeerById(chatId)
request.Bot = &tg.InputUser{
UserID: bot.ID,
AccessHash: bot.AccessHash,
}
return ctx.Client.MessagesGetInlineBotResults(ctx, request)
}

// TODO: Implement return helper for inline bot result

// SendInlineBotResult invokes method messages.sendInlineBotResult#7aa11297 returning error if any. Send a result obtained using messages.getInlineBotResults¹.
Expand Down Expand Up @@ -222,8 +254,20 @@ func (ctx *Context) GetUser(userId int64) (*tg.UserFull, error) {
}

// GetMessages is used to fetch messages from a PM (Private Chat).
func (ctx *Context) GetMessages(messageIds []tg.InputMessageClass) ([]tg.MessageClass, error) {
return functions.GetMessages(ctx, ctx.Client, messageIds)
func (ctx *Context) GetMessages(chatId int64, messageIds []tg.InputMessageClass) ([]tg.MessageClass, error) {
peer := storage.GetPeerById(chatId)
if peer.ID == 0 {
return nil, ErrPeerNotFound
}
switch storage.EntityType(peer.Type) {
case storage.TypeChannel:
return functions.GetChannelMessages(ctx, ctx.Client, &tg.InputChannel{
ChannelID: peer.ID,
AccessHash: peer.AccessHash,
}, messageIds)
default:
return functions.GetChatMessages(ctx, ctx.Client, messageIds)
}
}

// BanChatMember is used to ban a user from a chat.
Expand Down Expand Up @@ -386,12 +430,12 @@ func (ctx *Context) UnarchiveChats(chatIds []int64) (bool, error) {
//
// Links:
// 1) https://core.telegram.org/api/channel
func (ctx *Context) CreateChannel(title, about string, broadcast bool) (tg.UpdatesClass, error) {
func (ctx *Context) CreateChannel(title, about string, broadcast bool) (*tg.Channel, error) {
return functions.CreateChannel(ctx, ctx.Client, title, about, broadcast)
}

// CreateChat invokes method messages.createChat#9cb126e returning error if any. Creates a new chat.
func (ctx *Context) CreateChat(title string, userIds []int64) (tg.UpdatesClass, error) {
func (ctx *Context) CreateChat(title string, userIds []int64) (*tg.Chat, error) {
userPeers := make([]tg.InputUserClass, len(userIds))
for i, uId := range userIds {
userPeer := storage.GetPeerById(uId)
Expand Down
14 changes: 14 additions & 0 deletions ext/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ext

import (
"fmt"
"strings"

"github.com/anonyindian/gotgproto/types"
"github.com/gotd/td/telegram/message"
Expand Down Expand Up @@ -59,6 +60,19 @@ func GetNewUpdate(e *tg.Entities, update tg.UpdateClass) *Update {
return u
}

func (u *Update) Args() []string {
switch {
case u.EffectiveMessage != nil:
return strings.Fields(u.EffectiveMessage.Message)
case u.CallbackQuery != nil:
return strings.Fields(string(u.CallbackQuery.Data))
case u.InlineQuery != nil:
return strings.Fields(u.InlineQuery.Query)
default:
return make([]string, 0)
}
}

// EffectiveUser returns the tg.User who is responsible for the update.
func (u *Update) EffectiveUser() *tg.User {
if u.Entities == nil {
Expand Down
21 changes: 17 additions & 4 deletions functions/chatHelplers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package functions

import (
"context"

"github.com/gotd/td/tg"
)

Expand Down Expand Up @@ -50,19 +51,31 @@ func ArchiveChats(context context.Context, client *tg.Client, peers []tg.InputPe
return err == nil, err
}

func CreateChannel(context context.Context, client *tg.Client, title, about string, broadcast bool) (tg.UpdatesClass, error) {
return client.ChannelsCreateChannel(context, &tg.ChannelsCreateChannelRequest{
func CreateChannel(context context.Context, client *tg.Client, title, about string, broadcast bool) (*tg.Channel, error) {
udps, err := client.ChannelsCreateChannel(context, &tg.ChannelsCreateChannelRequest{
Title: title,
About: about,
Broadcast: broadcast,
})
if err != nil {
return nil, err
}
// Highly experimental value from ChatClass array
_, chats, _ := getUpdateFromUpdates(udps)
return chats[0].(*tg.Channel), nil
}

func CreateChat(context context.Context, client *tg.Client, title string, users []tg.InputUserClass) (tg.UpdatesClass, error) {
return client.MessagesCreateChat(context, &tg.MessagesCreateChatRequest{
func CreateChat(context context.Context, client *tg.Client, title string, users []tg.InputUserClass) (*tg.Chat, error) {
udps, err := client.MessagesCreateChat(context, &tg.MessagesCreateChatRequest{
Users: users,
Title: title,
})
if err != nil {
return nil, err
}
// Highly experimental value from ChatClass map
_, chats, _ := getUpdateFromUpdates(udps)
return chats[0].(*tg.Chat), nil
}

func BanChatMember(context context.Context, client *tg.Client, chatPeer tg.InputPeerClass, userPeer *tg.InputPeerUser, untilDate int) (tg.UpdatesClass, error) {
Expand Down
28 changes: 27 additions & 1 deletion functions/messageHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,43 @@ import (
"github.com/gotd/td/tg"
)

func GetMessages(context context.Context, client *tg.Client, messageIds []tg.InputMessageClass) (tg.MessageClassArray, error) {
func GetChannelMessages(context context.Context, client *tg.Client, peer tg.InputChannelClass, messageIds []tg.InputMessageClass) (tg.MessageClassArray, error) {
messages, err := client.ChannelsGetMessages(context, &tg.ChannelsGetMessagesRequest{
Channel: peer,
ID: messageIds,
})
if err != nil {
return nil, err
}
switch m := messages.(type) {
case *tg.MessagesMessages:
go SavePeersFromClassArray(m.Chats, m.Users)
return m.Messages, nil
case *tg.MessagesMessagesSlice:
go SavePeersFromClassArray(m.Chats, m.Users)
return m.Messages, nil
case *tg.MessagesChannelMessages:
go SavePeersFromClassArray(m.Chats, m.Users)
return m.Messages, nil
default:
return nil, nil
}
}

func GetChatMessages(context context.Context, client *tg.Client, messageIds []tg.InputMessageClass) (tg.MessageClassArray, error) {
messages, err := client.MessagesGetMessages(context, messageIds)
if err != nil {
return nil, err
}
switch m := messages.(type) {
case *tg.MessagesMessages:
go SavePeersFromClassArray(m.Chats, m.Users)
return m.Messages, nil
case *tg.MessagesMessagesSlice:
go SavePeersFromClassArray(m.Chats, m.Users)
return m.Messages, nil
case *tg.MessagesChannelMessages:
go SavePeersFromClassArray(m.Chats, m.Users)
return m.Messages, nil
default:
return nil, nil
Expand Down
34 changes: 19 additions & 15 deletions functions/peerHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,7 @@ func ExtractContactResolvedPeer(p *tg.ContactsResolvedPeer, err error) (types.Ef
if err != nil {
return &types.EmptyUC{}, err
}
go func() {
for _, chat := range p.Chats {
switch chat := chat.(type) {
case *tg.Channel:
storage.AddPeer(chat.ID, chat.AccessHash, storage.TypeChannel, chat.Username)
}
}
for _, user := range p.Users {
user, ok := user.(*tg.User)
if !ok {
continue
}
storage.AddPeer(user.ID, user.AccessHash, storage.TypeUser, user.Username)
}
}()
go SavePeersFromClassArray(p.Chats, p.Users)
switch p.Peer.(type) {
case *tg.PeerChannel:
if p.Chats == nil || len(p.Chats) == 0 {
Expand Down Expand Up @@ -113,3 +99,21 @@ func GetInputPeerClassFromId(iD int64) tg.InputPeerClass {
}
return nil
}

func SavePeersFromClassArray(cs []tg.ChatClass, us []tg.UserClass) {
for _, u := range us {
u, ok := u.(*tg.User)
if !ok {
continue
}
storage.AddPeer(u.ID, u.AccessHash, storage.TypeUser, u.Username)
}
for _, c := range cs {
switch c := c.(type) {
case *tg.Channel:
storage.AddPeer(c.ID, c.AccessHash, storage.TypeChannel, c.Username)
case *tg.Chat:
storage.AddPeer(c.ID, storage.DefaultAccessHash, storage.TypeChat, storage.DefaultUsername)
}
}
}
21 changes: 12 additions & 9 deletions functions/updatesHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ func GetEditMessageUpdate(upds tg.UpdatesClass) *tg.Message {
return nil
}

func GetUpdateClassFromUpdatesClass(updates tg.UpdatesClass) []tg.UpdateClass {
var (
upds []tg.UpdateClass
)
func GetUpdateClassFromUpdatesClass(updates tg.UpdatesClass) (u []tg.UpdateClass) {
u, _, _ = getUpdateFromUpdates(updates)
return
}

func getUpdateFromUpdates(updates tg.UpdatesClass) ([]tg.UpdateClass, []tg.ChatClass, []tg.UserClass) {
switch u := updates.(type) {
case *tg.Updates:
upds = u.Updates
go SavePeersFromClassArray(u.Chats, u.Users)
return u.Updates, u.Chats, u.Users
case *tg.UpdatesCombined:
upds = u.Updates
go SavePeersFromClassArray(u.Chats, u.Users)
return u.Updates, u.Chats, u.Users
case *tg.UpdateShort:
upds = []tg.UpdateClass{u.Update}
return []tg.UpdateClass{u.Update}, tg.ChatClassArray{}, tg.UserClassArray{}
default:
return nil
return nil, nil, nil
}
return upds
}

func GetMessageFromMessageClass(m tg.MessageClass) *tg.Message {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/anonyindian/gotgproto
go 1.17

require (
github.com/allegro/bigcache v1.2.1
github.com/go-faster/errors v0.6.1
github.com/gotd/td v0.62.0
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/allegro/bigcache v1.2.1 h1:hg1sY1raCwic3Vnsvje6TT7/pnZba83LeFck5NrFKSc=
github.com/allegro/bigcache v1.2.1/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129/go.mod h1:rFgpPQZYZ8vdbc+48xibu8ALc3yeyd64IhHS+PU6Yyg=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
Expand Down
Loading

0 comments on commit 79b5b8c

Please sign in to comment.