mirror of
https://github.com/c0re100/gotdlib.git
synced 2026-02-22 04:30:17 +01:00
Compare commits
84 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9c7701a0d | ||
|
|
0f088c4101 | ||
|
|
c1bd67f199 | ||
|
|
4597e26403 | ||
|
|
926224f707 | ||
|
|
9b94728dda | ||
|
|
14418433a4 | ||
|
|
5c5078ec42 | ||
|
|
51f3ce0659 | ||
|
|
bc2b5f5823 | ||
|
|
969ddb4746 | ||
|
|
e5eeec83b3 | ||
|
|
3a8d30fd35 | ||
|
|
eb767ed26e | ||
|
|
b943b2fe5e | ||
|
|
dc9ae3ed54 | ||
|
|
c695d13f46 | ||
|
|
05b67218a9 | ||
|
|
8de0893227 | ||
|
|
baee9b059d | ||
|
|
bda4018ed3 | ||
|
|
2a5a6d2b76 | ||
|
|
626ffe1a7b | ||
|
|
58adcc4804 | ||
|
|
930f3352f3 | ||
|
|
4b010c3b43 | ||
|
|
098715f4f0 | ||
|
|
4330cb2fa1 | ||
|
|
8454bd8c86 | ||
|
|
ec1fb9c5e2 | ||
|
|
23e26078e9 | ||
|
|
4b5b0a30a0 | ||
|
|
fa003a9460 | ||
|
|
1f84ff6e15 | ||
|
|
fefab36108 | ||
|
|
b75bf70673 | ||
|
|
b5f52a79a6 | ||
|
|
3052b01106 | ||
|
|
303a126830 | ||
|
|
0465eebee7 | ||
|
|
7005072ba4 | ||
|
|
97ffe5213a | ||
|
|
8f4de4d76e | ||
|
|
e30af65ec7 | ||
|
|
af41176160 | ||
|
|
563cd7d677 | ||
|
|
ad108a3ee2 | ||
|
|
7563ce479c | ||
|
|
434963f2c9 | ||
|
|
4459ee0f55 | ||
|
|
9a3301fbd7 | ||
|
|
83df5e2abb | ||
|
|
9e42c4a4ee | ||
|
|
af2b93f686 | ||
|
|
26f72d96ce | ||
|
|
0b8a2f54dd | ||
|
|
7a1c7f37a2 | ||
|
|
e722882047 | ||
|
|
47887c89e1 | ||
|
|
993b734553 | ||
|
|
d0f51e73ee | ||
|
|
869b97df28 | ||
|
|
7df275842c | ||
|
|
712b01c11c | ||
|
|
abdd0dfd48 | ||
|
|
65692be746 | ||
|
|
3b10777734 | ||
|
|
0b0467fbb7 | ||
|
|
fffcf86198 | ||
|
|
51b9da9304 | ||
|
|
52c9a63fab | ||
|
|
d5cf5072c8 | ||
|
|
bbfcb2f946 | ||
|
|
1011dc13ae | ||
|
|
94b077458b | ||
|
|
36a547a560 | ||
|
|
f70d5bf7e6 | ||
|
|
1d7608dd7c | ||
|
|
f312f1cc07 | ||
|
|
f36c0db78b | ||
|
|
6400f59b1b | ||
|
|
a339293774 | ||
|
|
a7ee5501e3 | ||
|
|
cc7712e2c4 |
22 changed files with 125430 additions and 48686 deletions
|
|
@ -7,8 +7,9 @@ I realized that zelenin's library doesn't meet my need😕
|
||||||
So I fork it and make some changes
|
So I fork it and make some changes
|
||||||
|
|
||||||
1. Static build by default
|
1. Static build by default
|
||||||
2. Add update event filter
|
2. Add update [event filter](example#event-filter)
|
||||||
3. Add command parser
|
3. Add [command](example#command) parser
|
||||||
4. Receive correct message id to patch text/dice message response.
|
4. Receive correct message id to patch text/dice message response.
|
||||||
|
5. Add [Pending updates](example#pending-updates)
|
||||||
|
|
||||||
[Here](example) are a few example codes about how to use **c0re100/gotdlib**.
|
[Here](example) are a few example codes about how to use **c0re100/gotdlib**.
|
||||||
|
|
@ -3,7 +3,10 @@ package client
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrNotSupportedAuthorizationState = errors.New("not supported state")
|
var ErrNotSupportedAuthorizationState = errors.New("not supported state")
|
||||||
|
|
@ -43,7 +46,7 @@ func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandl
|
||||||
}
|
}
|
||||||
|
|
||||||
type clientAuthorizer struct {
|
type clientAuthorizer struct {
|
||||||
TdlibParameters chan *TdlibParameters
|
TdlibParameters chan *SetTdlibParametersRequest
|
||||||
PhoneNumber chan string
|
PhoneNumber chan string
|
||||||
Code chan string
|
Code chan string
|
||||||
State chan AuthorizationState
|
State chan AuthorizationState
|
||||||
|
|
@ -52,7 +55,7 @@ type clientAuthorizer struct {
|
||||||
|
|
||||||
func ClientAuthorizer() *clientAuthorizer {
|
func ClientAuthorizer() *clientAuthorizer {
|
||||||
return &clientAuthorizer{
|
return &clientAuthorizer{
|
||||||
TdlibParameters: make(chan *TdlibParameters, 1),
|
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
|
||||||
PhoneNumber: make(chan string, 1),
|
PhoneNumber: make(chan string, 1),
|
||||||
Code: make(chan string, 1),
|
Code: make(chan string, 1),
|
||||||
State: make(chan AuthorizationState, 10),
|
State: make(chan AuthorizationState, 10),
|
||||||
|
|
@ -65,13 +68,7 @@ func (stateHandler *clientAuthorizer) Handle(client *Client, state Authorization
|
||||||
|
|
||||||
switch state.AuthorizationStateType() {
|
switch state.AuthorizationStateType() {
|
||||||
case TypeAuthorizationStateWaitTdlibParameters:
|
case TypeAuthorizationStateWaitTdlibParameters:
|
||||||
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
|
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||||
Parameters: <-stateHandler.TdlibParameters,
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitEncryptionKey:
|
|
||||||
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitPhoneNumber:
|
case TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
|
@ -150,10 +147,13 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitPassword:
|
case TypeAuthorizationStateWaitPassword:
|
||||||
fmt.Println("Enter password: ")
|
fmt.Println("Enter password: ")
|
||||||
var password string
|
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
|
||||||
fmt.Scanln(&password)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
clientAuthorizer.Password <- password
|
clientAuthorizer.Password <- string(bytePassword)
|
||||||
|
|
||||||
case TypeAuthorizationStateReady:
|
case TypeAuthorizationStateReady:
|
||||||
return
|
return
|
||||||
|
|
@ -163,14 +163,14 @@ func CliInteractor(clientAuthorizer *clientAuthorizer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type botAuthorizer struct {
|
type botAuthorizer struct {
|
||||||
TdlibParameters chan *TdlibParameters
|
TdlibParameters chan *SetTdlibParametersRequest
|
||||||
Token chan string
|
Token chan string
|
||||||
State chan AuthorizationState
|
State chan AuthorizationState
|
||||||
}
|
}
|
||||||
|
|
||||||
func BotAuthorizer(token string) *botAuthorizer {
|
func BotAuthorizer(token string) *botAuthorizer {
|
||||||
botAuthorizer := &botAuthorizer{
|
botAuthorizer := &botAuthorizer{
|
||||||
TdlibParameters: make(chan *TdlibParameters, 1),
|
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
|
||||||
Token: make(chan string, 1),
|
Token: make(chan string, 1),
|
||||||
State: make(chan AuthorizationState, 10),
|
State: make(chan AuthorizationState, 10),
|
||||||
}
|
}
|
||||||
|
|
@ -185,13 +185,7 @@ func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationSta
|
||||||
|
|
||||||
switch state.AuthorizationStateType() {
|
switch state.AuthorizationStateType() {
|
||||||
case TypeAuthorizationStateWaitTdlibParameters:
|
case TypeAuthorizationStateWaitTdlibParameters:
|
||||||
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
|
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
||||||
Parameters: <-stateHandler.TdlibParameters,
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitEncryptionKey:
|
|
||||||
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
case TypeAuthorizationStateWaitPhoneNumber:
|
case TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
|
|
||||||
101
client/client.go
101
client/client.go
|
|
@ -9,15 +9,19 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var pendingUpdateType []Type
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
jsonClient *JsonClient
|
jsonClient *JsonClient
|
||||||
extraGenerator ExtraGenerator
|
extraGenerator ExtraGenerator
|
||||||
responses chan *Response
|
responses chan *Response
|
||||||
|
pendingResp chan *Response
|
||||||
listenerStore *listenerStore
|
listenerStore *listenerStore
|
||||||
catchersStore *sync.Map
|
catchersStore *sync.Map
|
||||||
successMsgStore *sync.Map
|
successMsgStore *sync.Map
|
||||||
updatesTimeout time.Duration
|
updatesTimeout time.Duration
|
||||||
catchTimeout time.Duration
|
catchTimeout time.Duration
|
||||||
|
DisablePatch bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Client)
|
type Option func(*Client)
|
||||||
|
|
@ -40,6 +44,12 @@ func WithProxy(req *AddProxyRequest) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithoutSendMessagePatch() Option {
|
||||||
|
return func(client *Client) {
|
||||||
|
client.DisablePatch = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func SetLogLevel(level int32) {
|
func SetLogLevel(level int32) {
|
||||||
_, _ = SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{
|
_, _ = SetLogVerbosityLevel(&SetLogVerbosityLevelRequest{
|
||||||
NewVerbosityLevel: level,
|
NewVerbosityLevel: level,
|
||||||
|
|
@ -56,10 +66,18 @@ func SetFilePath(path string) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Keep specific update type in memory when listener is not ready.
|
||||||
|
func SetPendingUpdateType(update ...Type) {
|
||||||
|
for _, v := range update {
|
||||||
|
pendingUpdateType = append(pendingUpdateType, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...Option) (*Client, error) {
|
||||||
client := &Client{
|
client := &Client{
|
||||||
jsonClient: NewJsonClient(),
|
jsonClient: NewJsonClient(),
|
||||||
responses: make(chan *Response, 1000),
|
responses: make(chan *Response, 1000),
|
||||||
|
pendingResp: make(chan *Response, 1000),
|
||||||
listenerStore: newListenerStore(),
|
listenerStore: newListenerStore(),
|
||||||
catchersStore: &sync.Map{},
|
catchersStore: &sync.Map{},
|
||||||
successMsgStore: &sync.Map{},
|
successMsgStore: &sync.Map{},
|
||||||
|
|
@ -68,14 +86,15 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
||||||
client.extraGenerator = UuidV4Generator()
|
client.extraGenerator = UuidV4Generator()
|
||||||
client.catchTimeout = 60 * time.Second
|
client.catchTimeout = 60 * time.Second
|
||||||
|
|
||||||
for _, option := range options {
|
|
||||||
option(client)
|
|
||||||
}
|
|
||||||
|
|
||||||
tdlibInstance.addClient(client)
|
tdlibInstance.addClient(client)
|
||||||
|
|
||||||
|
go client.processPendingResponse()
|
||||||
go client.receiver()
|
go client.receiver()
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
go option(client)
|
||||||
|
}
|
||||||
|
|
||||||
err := Authorize(client, authorizationStateHandler)
|
err := Authorize(client, authorizationStateHandler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -84,8 +103,7 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) receiver() {
|
func (client *Client) processResponse(response *Response) {
|
||||||
for response := range client.responses {
|
|
||||||
if response.Extra != "" {
|
if response.Extra != "" {
|
||||||
value, ok := client.catchersStore.Load(response.Extra)
|
value, ok := client.catchersStore.Load(response.Extra)
|
||||||
if ok {
|
if ok {
|
||||||
|
|
@ -95,20 +113,39 @@ func (client *Client) receiver() {
|
||||||
|
|
||||||
typ, err := UnmarshalType(response.Data)
|
typ, err := UnmarshalType(response.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
|
if !client.DisablePatch && typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
|
||||||
value, ok := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
|
sendVal, sOk := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
|
||||||
if ok {
|
if sOk {
|
||||||
value.(chan *Response) <- response
|
sendVal.(chan *Response) <- response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(client.listenerStore.Listeners()) == 0 {
|
||||||
|
for _, p := range pendingUpdateType {
|
||||||
|
if typ.GetType() == p.GetType() {
|
||||||
|
client.pendingResp <- response
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
needGc := false
|
needGc := false
|
||||||
for _, listener := range client.listenerStore.Listeners() {
|
for _, listener := range client.listenerStore.Listeners() {
|
||||||
if listener.IsActive() && listener.Updates != nil && typ.GetType() == listener.Filter.GetType() { // All updates go to Updates channel if type == filter
|
if listener.IsActive() && listener.Updates != nil && typ.GetType() == listener.Filter.GetType() { // All updates go to Updates channel if type == filter
|
||||||
|
// Make some delay to UpdateMessageSendSucceeded listener
|
||||||
|
// This can make UpdateMessageSendSucceeded response later than sendMessage response.
|
||||||
|
// This may help a bot developer to map temporary message id to actual message id easily.
|
||||||
|
// Cause an event listener slower than sendMessage response, so you have enough time to do mapping stuff.
|
||||||
|
if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
|
||||||
|
go func(listener *Listener, typ Type) {
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
listener.Updates <- typ
|
listener.Updates <- typ
|
||||||
|
}(listener, typ)
|
||||||
|
} else {
|
||||||
|
listener.Updates <- typ
|
||||||
|
}
|
||||||
} else if listener.IsActive() && listener.RawUpdates != nil { // All updates go to RawUpdates channel if filter is empty
|
} else if listener.IsActive() && listener.RawUpdates != nil { // All updates go to RawUpdates channel if filter is empty
|
||||||
listener.RawUpdates <- typ
|
listener.RawUpdates <- typ
|
||||||
} else if !listener.IsActive() { // GC inactive listener
|
} else if !listener.IsActive() { // GC inactive listener
|
||||||
|
|
@ -118,6 +155,35 @@ func (client *Client) receiver() {
|
||||||
if needGc {
|
if needGc {
|
||||||
client.listenerStore.gc()
|
client.listenerStore.gc()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if typ.GetType() == TypeUpdateAuthorizationState && typ.(*UpdateAuthorizationState).AuthorizationState.AuthorizationStateType() == TypeAuthorizationStateClosed {
|
||||||
|
close(client.responses)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) receiver() {
|
||||||
|
for response := range client.responses {
|
||||||
|
client.processResponse(response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (client *Client) processPendingResponse() {
|
||||||
|
// No need to process pending response if no pending list.
|
||||||
|
if len(pendingUpdateType) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for listener to be ready.
|
||||||
|
for {
|
||||||
|
if len(client.listenerStore.Listeners()) > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start processing pending response
|
||||||
|
for response := range client.pendingResp {
|
||||||
|
client.processResponse(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,7 +206,7 @@ func (client *Client) Send(req Request) (*Response, error) {
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case response := <-catcher:
|
case response := <-catcher:
|
||||||
if response.Type != "error" && req.Type == "sendMessage" {
|
if !client.DisablePatch && response.Type != "error" && req.Type == "sendMessage" {
|
||||||
m, err := UnmarshalMessage(response.Data)
|
m, err := UnmarshalMessage(response.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -161,12 +227,11 @@ func (client *Client) Send(req Request) (*Response, error) {
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
response.Data = bytes.Replace(response.Data, []byte("{\"@type\":\"messageSendingStatePending\"}"), []byte("{\"@type\":\"updateMessageSendSucceeded\"}"), 1)
|
response.Data = bytes.Replace(response.Data, []byte("\"@type\":\"messageSendingStatePending\""), []byte("\"@type\":\"updateMessageSendSucceeded\""), 1)
|
||||||
response.Data = bytes.Replace(response.Data, []byte(strconv.FormatInt(m.Id, 10)), []byte(strconv.FormatInt(m2.Message.Id, 10)), 1)
|
response.Data = bytes.Replace(response.Data, []byte("\"id\":"+strconv.FormatInt(m.Id, 10)), []byte("\"id\":"+strconv.FormatInt(m2.Message.Id, 10)), 1)
|
||||||
return response, nil
|
return response, nil
|
||||||
case <-time.After(1 * time.Second):
|
case <-time.After(1 * time.Second):
|
||||||
client.successMsgStore.Delete(m.Id)
|
return response, nil
|
||||||
close(successCatcher)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -196,7 +261,3 @@ func (client *Client) AddEventReceiver(msgType Type, channelCapacity int) *Liste
|
||||||
|
|
||||||
return listener
|
return listener
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Stop() {
|
|
||||||
client.Destroy()
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,67 +1,49 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtraGenerator func() string
|
type ExtraGenerator func() string
|
||||||
|
|
||||||
func UuidV4Generator() ExtraGenerator {
|
func UuidV4Generator() ExtraGenerator {
|
||||||
return func() string {
|
return func() string {
|
||||||
var uuid [16]byte
|
return uuid.NewString()
|
||||||
rand.Read(uuid[:])
|
|
||||||
|
|
||||||
uuid[6] = (uuid[6] & 0x0f) | 0x40
|
|
||||||
uuid[8] = (uuid[8] & 0x3f) | 0x80
|
|
||||||
|
|
||||||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsCommmand(text string) bool {
|
func IsCommand(text string) bool {
|
||||||
if text != "" {
|
if i := strings.Index(text, "/"); i == 0 {
|
||||||
if text[0] == '/' {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckCommand(text string, entities []*TextEntity) string {
|
func CheckCommand(text string, entities []*TextEntity) string {
|
||||||
if IsCommmand(text) {
|
if IsCommand(text) {
|
||||||
// Check text entities and make bot happy!
|
cmd := text
|
||||||
if len(entities) >= 1 {
|
|
||||||
// Get first command
|
|
||||||
if entities[0].Type.TextEntityTypeType() == "textEntityTypeBotCommand" {
|
|
||||||
// e.g.: { "text": "/hello@world_bot", "textEntity": { offset: 0, length: 16 } }
|
|
||||||
// Result: "/hello"
|
|
||||||
if i := strings.Index(text[:entities[0].Length], "@"); i != -1 {
|
|
||||||
return text[:i]
|
|
||||||
}
|
|
||||||
return text[:entities[0].Length]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Since userbot does not have bot command entities in Private Chat, so make userbot happy too!
|
|
||||||
// e.g.: ["/hello@world_bot", "/hello@", "/hello@123"]
|
|
||||||
// Result: "/hello"
|
|
||||||
if i := strings.Index(text, "@"); i != -1 {
|
|
||||||
return text[:i]
|
|
||||||
}
|
|
||||||
// e.g. ["/hello 123", "/hell o 123"]
|
// e.g. ["/hello 123", "/hell o 123"]
|
||||||
// Result: "/hello", "/hell"
|
// Result: "/hello", "/hell"
|
||||||
if i := strings.Index(text, " "); i != -1 {
|
if i := strings.Index(cmd, " "); i != -1 {
|
||||||
return text[:i]
|
cmd = cmd[:i]
|
||||||
}
|
}
|
||||||
return text
|
|
||||||
|
// e.g.: ["/hello@world_bot", "/hello@", "/hello@123"]
|
||||||
|
// Result: "/hello"
|
||||||
|
if i := strings.Index(cmd, "@"); i != -1 {
|
||||||
|
cmd = cmd[:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return cmd
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func CommandArgument(text string) string {
|
func CommandArgument(text string) string {
|
||||||
if IsCommmand(text) {
|
if IsCommand(text) {
|
||||||
// e.g. ["/hello 123", "/hell o 123"]
|
// e.g. ["/hello 123", "/hell o 123"]
|
||||||
// Result: "123", "o 123"
|
// Result: "123", "o 123"
|
||||||
if i := strings.Index(text, " "); i != -1 {
|
if i := strings.Index(text, " "); i != -1 {
|
||||||
|
|
|
||||||
17024
client/function.go
17024
client/function.go
File diff suppressed because it is too large
Load diff
|
|
@ -1,10 +1,12 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
//#cgo linux CFLAGS: -I/usr/local/include
|
//#cgo linux CFLAGS: -I/usr/local/include
|
||||||
|
//#cgo freebsd CFLAGS: -I/usr/local/include
|
||||||
//#cgo darwin CFLAGS: -I/usr/local/include
|
//#cgo darwin CFLAGS: -I/usr/local/include
|
||||||
//#cgo windows CFLAGS: -IE:/src/tdlib -IE:/src/tdlib/build
|
//#cgo windows CFLAGS: -IE:/src/tdlib -IE:/src/tdlib/build
|
||||||
//#cgo linux LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
//#cgo linux LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltde2e -ltdsqlite -ltdmtproto -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
||||||
//#cgo darwin LDFLAGS: -L/usr/local/lib -L/usr/local/opt/openssl/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltdsqlite -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
//#cgo freebsd LDFLAGS: -L/usr/local/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltde2e -ltdsqlite -ltdmtproto -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
||||||
|
//#cgo darwin LDFLAGS: -L/usr/local/lib -L/usr/local/opt/openssl/lib -ltdjson_static -ltdjson_private -ltdclient -ltdcore -ltdapi -ltdactor -ltddb -ltde2e -ltdsqlite -ltdmtproto -ltdnet -ltdutils -lstdc++ -lssl -lcrypto -ldl -lz -lm
|
||||||
//#cgo windows LDFLAGS: -LE:/src/tdlib/build/Release -ltdjson
|
//#cgo windows LDFLAGS: -LE:/src/tdlib/build/Release -ltdjson
|
||||||
//#include <stdlib.h>
|
//#include <stdlib.h>
|
||||||
//#include <td/telegram/td_json_client.h>
|
//#include <td/telegram/td_json_client.h>
|
||||||
|
|
|
||||||
39938
client/type.go
39938
client/type.go
File diff suppressed because it is too large
Load diff
17714
client/unmarshaler.go
17714
client/unmarshaler.go
File diff suppressed because it is too large
Load diff
11978
data/td_api.tl
11978
data/td_api.tl
File diff suppressed because it is too large
Load diff
|
|
@ -13,5 +13,10 @@ So we need to filter update events, like UpdateNewMessage, UpdateMessageSendSucc
|
||||||
### Media
|
### Media
|
||||||
Send photo or album to chat.
|
Send photo or album to chat.
|
||||||
|
|
||||||
|
### Pending updates
|
||||||
|
When starting a bot, we may have some updates that are missed to process when a listener IS NOT ready.
|
||||||
|
|
||||||
|
So we need to keep specific update types in memory until a listener is set, then we can process those updates again.
|
||||||
|
|
||||||
### Raw Update
|
### Raw Update
|
||||||
Get update without event filter.
|
Get update without event filter.
|
||||||
|
|
@ -9,8 +9,8 @@ import (
|
||||||
tdlib "github.com/c0re100/gotdlib/client"
|
tdlib "github.com/c0re100/gotdlib/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetTdParameters() *tdlib.TdlibParameters {
|
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
|
||||||
return &tdlib.TdlibParameters{
|
return &tdlib.SetTdlibParametersRequest{
|
||||||
UseTestDc: false,
|
UseTestDc: false,
|
||||||
DatabaseDirectory: "./tdlib-db",
|
DatabaseDirectory: "./tdlib-db",
|
||||||
FilesDirectory: "./tdlib-files",
|
FilesDirectory: "./tdlib-files",
|
||||||
|
|
@ -24,8 +24,6 @@ func GetTdParameters() *tdlib.TdlibParameters {
|
||||||
DeviceModel: "HuskyNG",
|
DeviceModel: "HuskyNG",
|
||||||
SystemVersion: "3.0",
|
SystemVersion: "3.0",
|
||||||
ApplicationVersion: "3.0",
|
ApplicationVersion: "3.0",
|
||||||
EnableStorageOptimizer: true,
|
|
||||||
IgnoreFileNames: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,7 +50,7 @@ func main() {
|
||||||
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
||||||
go func() {
|
go func() {
|
||||||
<-ch
|
<-ch
|
||||||
client.Destroy()
|
client.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
me, err := client.GetMe()
|
me, err := client.GetMe()
|
||||||
|
|
@ -60,7 +58,7 @@ func main() {
|
||||||
log.Fatalf("GetMe error: %s", err)
|
log.Fatalf("GetMe error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s connected", me.Username)
|
log.Printf("%v connected", me.Usernames)
|
||||||
|
|
||||||
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
||||||
|
|
||||||
|
|
@ -87,7 +85,9 @@ func main() {
|
||||||
})
|
})
|
||||||
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
||||||
ChatId: chatId,
|
ChatId: chatId,
|
||||||
ReplyToMessageId: msgId,
|
ReplyTo: &tdlib.InputMessageReplyToMessage{
|
||||||
|
MessageId: msgId,
|
||||||
|
},
|
||||||
InputMessageContent: &tdlib.InputMessageText{
|
InputMessageContent: &tdlib.InputMessageText{
|
||||||
Text: text,
|
Text: text,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ func GetSenderId(sender tdlib.MessageSender) int64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTdParameters() *tdlib.TdlibParameters {
|
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
|
||||||
return &tdlib.TdlibParameters{
|
return &tdlib.SetTdlibParametersRequest{
|
||||||
UseTestDc: false,
|
UseTestDc: false,
|
||||||
DatabaseDirectory: "./tdlib-db",
|
DatabaseDirectory: "./tdlib-db",
|
||||||
FilesDirectory: "./tdlib-files",
|
FilesDirectory: "./tdlib-files",
|
||||||
|
|
@ -32,8 +32,6 @@ func GetTdParameters() *tdlib.TdlibParameters {
|
||||||
DeviceModel: "HuskyNG",
|
DeviceModel: "HuskyNG",
|
||||||
SystemVersion: "3.0",
|
SystemVersion: "3.0",
|
||||||
ApplicationVersion: "3.0",
|
ApplicationVersion: "3.0",
|
||||||
EnableStorageOptimizer: true,
|
|
||||||
IgnoreFileNames: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +58,7 @@ func main() {
|
||||||
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
||||||
go func() {
|
go func() {
|
||||||
<-ch
|
<-ch
|
||||||
client.Destroy()
|
client.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
me, err := client.GetMe()
|
me, err := client.GetMe()
|
||||||
|
|
@ -68,7 +66,7 @@ func main() {
|
||||||
log.Fatalf("GetMe error: %s", err)
|
log.Fatalf("GetMe error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s connected", me.Username)
|
log.Printf("%v connected", me.Usernames)
|
||||||
|
|
||||||
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
||||||
|
|
||||||
|
|
@ -98,7 +96,9 @@ func main() {
|
||||||
})
|
})
|
||||||
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
||||||
ChatId: chatId,
|
ChatId: chatId,
|
||||||
ReplyToMessageId: msgId,
|
ReplyTo: &tdlib.InputMessageReplyToMessage{
|
||||||
|
MessageId: msgId,
|
||||||
|
},
|
||||||
InputMessageContent: &tdlib.InputMessageText{
|
InputMessageContent: &tdlib.InputMessageText{
|
||||||
Text: text,
|
Text: text,
|
||||||
},
|
},
|
||||||
|
|
@ -107,6 +107,20 @@ func main() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Printf("Message sent, ID: %d", m.Id)
|
log.Printf("Message sent, ID: %d", m.Id)
|
||||||
|
case "/repeat":
|
||||||
|
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
||||||
|
ChatId: chatId,
|
||||||
|
ReplyTo: &tdlib.InputMessageReplyToMessage{
|
||||||
|
MessageId: msgId,
|
||||||
|
},
|
||||||
|
InputMessageContent: &tdlib.InputMessageText{
|
||||||
|
Text: &tdlib.FormattedText{Text: tdlib.CommandArgument(msgText)},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Printf("Message sent, ID: %d", m.Id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ func GetSenderId(sender tdlib.MessageSender) int64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTdParameters() *tdlib.TdlibParameters {
|
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
|
||||||
return &tdlib.TdlibParameters{
|
return &tdlib.SetTdlibParametersRequest{
|
||||||
UseTestDc: false,
|
UseTestDc: false,
|
||||||
DatabaseDirectory: "./tdlib-db",
|
DatabaseDirectory: "./tdlib-db",
|
||||||
FilesDirectory: "./tdlib-files",
|
FilesDirectory: "./tdlib-files",
|
||||||
|
|
@ -32,8 +32,6 @@ func GetTdParameters() *tdlib.TdlibParameters {
|
||||||
DeviceModel: "HuskyNG",
|
DeviceModel: "HuskyNG",
|
||||||
SystemVersion: "3.0",
|
SystemVersion: "3.0",
|
||||||
ApplicationVersion: "3.0",
|
ApplicationVersion: "3.0",
|
||||||
EnableStorageOptimizer: true,
|
|
||||||
IgnoreFileNames: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +58,7 @@ func main() {
|
||||||
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
||||||
go func() {
|
go func() {
|
||||||
<-ch
|
<-ch
|
||||||
client.Destroy()
|
client.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
me, err := client.GetMe()
|
me, err := client.GetMe()
|
||||||
|
|
@ -68,7 +66,7 @@ func main() {
|
||||||
log.Fatalf("GetMe error: %s", err)
|
log.Fatalf("GetMe error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s connected", me.Username)
|
log.Printf("%v connected", me.Usernames)
|
||||||
|
|
||||||
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
||||||
|
|
||||||
|
|
|
||||||
BIN
example/media/Meru_01.png
Normal file
BIN
example/media/Meru_01.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 794 KiB |
BIN
example/media/Meru_02.png
Normal file
BIN
example/media/Meru_02.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 900 KiB |
|
|
@ -17,8 +17,8 @@ func GetSenderId(sender tdlib.MessageSender) int64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTdParameters() *tdlib.TdlibParameters {
|
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
|
||||||
return &tdlib.TdlibParameters{
|
return &tdlib.SetTdlibParametersRequest{
|
||||||
UseTestDc: false,
|
UseTestDc: false,
|
||||||
DatabaseDirectory: "./tdlib-db",
|
DatabaseDirectory: "./tdlib-db",
|
||||||
FilesDirectory: "./tdlib-files",
|
FilesDirectory: "./tdlib-files",
|
||||||
|
|
@ -32,8 +32,6 @@ func GetTdParameters() *tdlib.TdlibParameters {
|
||||||
DeviceModel: "HuskyNG",
|
DeviceModel: "HuskyNG",
|
||||||
SystemVersion: "3.0",
|
SystemVersion: "3.0",
|
||||||
ApplicationVersion: "3.0",
|
ApplicationVersion: "3.0",
|
||||||
EnableStorageOptimizer: true,
|
|
||||||
IgnoreFileNames: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +58,7 @@ func main() {
|
||||||
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
||||||
go func() {
|
go func() {
|
||||||
<-ch
|
<-ch
|
||||||
client.Destroy()
|
client.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
me, err := client.GetMe()
|
me, err := client.GetMe()
|
||||||
|
|
@ -68,7 +66,7 @@ func main() {
|
||||||
log.Fatalf("GetMe error: %s", err)
|
log.Fatalf("GetMe error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s connected", me.Username)
|
log.Printf("%v connected", me.Usernames)
|
||||||
|
|
||||||
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
||||||
|
|
||||||
|
|
@ -98,10 +96,12 @@ func main() {
|
||||||
})
|
})
|
||||||
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
||||||
ChatId: chatId,
|
ChatId: chatId,
|
||||||
ReplyToMessageId: msgId,
|
ReplyTo: &tdlib.InputMessageReplyToMessage{
|
||||||
|
MessageId: msgId,
|
||||||
|
},
|
||||||
InputMessageContent: &tdlib.InputMessagePhoto{
|
InputMessageContent: &tdlib.InputMessagePhoto{
|
||||||
Photo: &tdlib.InputFileLocal{
|
Photo: &tdlib.InputFileLocal{
|
||||||
Path: "./myht9-1486821485193084928.jpg",
|
Path: "./Meru_01.png",
|
||||||
},
|
},
|
||||||
Caption: text,
|
Caption: text,
|
||||||
},
|
},
|
||||||
|
|
@ -117,17 +117,19 @@ func main() {
|
||||||
})
|
})
|
||||||
m, err := client.SendMessageAlbum(&tdlib.SendMessageAlbumRequest{
|
m, err := client.SendMessageAlbum(&tdlib.SendMessageAlbumRequest{
|
||||||
ChatId: chatId,
|
ChatId: chatId,
|
||||||
ReplyToMessageId: msgId,
|
ReplyTo: &tdlib.InputMessageReplyToMessage{
|
||||||
|
MessageId: msgId,
|
||||||
|
},
|
||||||
InputMessageContents: []tdlib.InputMessageContent{
|
InputMessageContents: []tdlib.InputMessageContent{
|
||||||
&tdlib.InputMessagePhoto{
|
&tdlib.InputMessagePhoto{
|
||||||
Photo: &tdlib.InputFileLocal{
|
Photo: &tdlib.InputFileLocal{
|
||||||
Path: "./myht9-1486821485193084928.jpg",
|
Path: "./Meru_01.png",
|
||||||
},
|
},
|
||||||
Caption: text,
|
Caption: text,
|
||||||
},
|
},
|
||||||
&tdlib.InputMessagePhoto{
|
&tdlib.InputMessagePhoto{
|
||||||
Photo: &tdlib.InputFileLocal{
|
Photo: &tdlib.InputFileLocal{
|
||||||
Path: "./hisagi_02-1486983199280738309.jpg",
|
Path: "./Meru_02.png",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 153 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 116 KiB |
107
example/pending/PendingUpdate.go
Normal file
107
example/pending/PendingUpdate.go
Normal file
|
|
@ -0,0 +1,107 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
tdlib "github.com/c0re100/gotdlib/client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
|
||||||
|
return &tdlib.SetTdlibParametersRequest{
|
||||||
|
UseTestDc: false,
|
||||||
|
DatabaseDirectory: "./tdlib-db",
|
||||||
|
FilesDirectory: "./tdlib-files",
|
||||||
|
UseFileDatabase: true,
|
||||||
|
UseChatInfoDatabase: true,
|
||||||
|
UseMessageDatabase: true,
|
||||||
|
UseSecretChats: false,
|
||||||
|
ApiId: 132712,
|
||||||
|
ApiHash: "e82c07ad653399a37baca8d1e498e472",
|
||||||
|
SystemLanguageCode: "en",
|
||||||
|
DeviceModel: "HuskyNG",
|
||||||
|
SystemVersion: "3.0",
|
||||||
|
ApplicationVersion: "3.0",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tdlib.SetLogLevel(0)
|
||||||
|
tdlib.SetFilePath("./errors.txt")
|
||||||
|
|
||||||
|
// Set pending update list
|
||||||
|
tdlib.SetPendingUpdateType(&tdlib.UpdateNewMessage{})
|
||||||
|
// Of coz, you can set more than one type, depending on your needs
|
||||||
|
//tdlib.SetPendingUpdateType(&tdlib.UpdateNewMessage{}, &tdlib.UpdateMessageEdited{}, &tdlib.UpdateDeleteMessages{})
|
||||||
|
|
||||||
|
botToken := "your_bot_token"
|
||||||
|
authorizer := tdlib.BotAuthorizer(botToken)
|
||||||
|
|
||||||
|
authorizer.TdlibParameters <- GetTdParameters()
|
||||||
|
|
||||||
|
client, err := tdlib.NewClient(authorizer)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("NewClient error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle SIGINT
|
||||||
|
ch := make(chan os.Signal, 2)
|
||||||
|
signal.Notify(ch, os.Interrupt, syscall.SIGINT)
|
||||||
|
signal.Notify(ch, os.Interrupt, syscall.SIGKILL)
|
||||||
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
|
||||||
|
signal.Notify(ch, os.Interrupt, syscall.SIGQUIT)
|
||||||
|
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
||||||
|
go func() {
|
||||||
|
<-ch
|
||||||
|
client.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
me, err := client.GetMe()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("GetMe error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("%v connected", me.Usernames)
|
||||||
|
|
||||||
|
listener := client.AddEventReceiver(&tdlib.UpdateNewMessage{}, 1000)
|
||||||
|
|
||||||
|
defer listener.Close()
|
||||||
|
for update := range listener.Updates {
|
||||||
|
updateMsg := update.(*tdlib.UpdateNewMessage)
|
||||||
|
chatId := updateMsg.Message.ChatId
|
||||||
|
msgId := updateMsg.Message.Id
|
||||||
|
|
||||||
|
var msgText string
|
||||||
|
var msgEnt []*tdlib.TextEntity
|
||||||
|
|
||||||
|
switch updateMsg.Message.Content.MessageContentType() {
|
||||||
|
case "messageText":
|
||||||
|
msgText = updateMsg.Message.Content.(*tdlib.MessageText).Text.Text
|
||||||
|
msgEnt = updateMsg.Message.Content.(*tdlib.MessageText).Text.Entities
|
||||||
|
|
||||||
|
cmd := tdlib.CheckCommand(msgText, msgEnt)
|
||||||
|
switch cmd {
|
||||||
|
case "/ping":
|
||||||
|
text, _ := tdlib.ParseTextEntities(&tdlib.ParseTextEntitiesRequest{
|
||||||
|
Text: "<b>pong!</b>",
|
||||||
|
ParseMode: &tdlib.TextParseModeHTML{},
|
||||||
|
})
|
||||||
|
m, err := client.SendMessage(&tdlib.SendMessageRequest{
|
||||||
|
ChatId: chatId,
|
||||||
|
ReplyTo: &tdlib.InputMessageReplyToMessage{
|
||||||
|
MessageId: msgId,
|
||||||
|
},
|
||||||
|
InputMessageContent: &tdlib.InputMessageText{
|
||||||
|
Text: text,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Printf("Message sent, ID: %d", m.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,8 +17,8 @@ func GetSenderId(sender tdlib.MessageSender) int64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTdParameters() *tdlib.TdlibParameters {
|
func GetTdParameters() *tdlib.SetTdlibParametersRequest {
|
||||||
return &tdlib.TdlibParameters{
|
return &tdlib.SetTdlibParametersRequest{
|
||||||
UseTestDc: false,
|
UseTestDc: false,
|
||||||
DatabaseDirectory: "./tdlib-db",
|
DatabaseDirectory: "./tdlib-db",
|
||||||
FilesDirectory: "./tdlib-files",
|
FilesDirectory: "./tdlib-files",
|
||||||
|
|
@ -32,8 +32,6 @@ func GetTdParameters() *tdlib.TdlibParameters {
|
||||||
DeviceModel: "HuskyNG",
|
DeviceModel: "HuskyNG",
|
||||||
SystemVersion: "3.0",
|
SystemVersion: "3.0",
|
||||||
ApplicationVersion: "3.0",
|
ApplicationVersion: "3.0",
|
||||||
EnableStorageOptimizer: true,
|
|
||||||
IgnoreFileNames: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,7 +58,7 @@ func main() {
|
||||||
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
signal.Notify(ch, os.Interrupt, syscall.SIGSEGV)
|
||||||
go func() {
|
go func() {
|
||||||
<-ch
|
<-ch
|
||||||
client.Destroy()
|
client.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
me, err := client.GetMe()
|
me, err := client.GetMe()
|
||||||
|
|
@ -68,7 +66,7 @@ func main() {
|
||||||
log.Fatalf("GetMe error: %s", err)
|
log.Fatalf("GetMe error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("%s connected", me.Username)
|
log.Printf("%v connected", me.Usernames)
|
||||||
|
|
||||||
listener := client.GetListener()
|
listener := client.GetListener()
|
||||||
|
|
||||||
|
|
|
||||||
5
go.mod
5
go.mod
|
|
@ -1,3 +1,8 @@
|
||||||
module github.com/c0re100/gotdlib
|
module github.com/c0re100/gotdlib
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
golang.org/x/crypto v0.31.0
|
||||||
|
)
|
||||||
|
|
|
||||||
69
go.sum
Normal file
69
go.sum
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||||
|
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||||
|
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||||
|
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||||
|
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||||
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||||
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||||
|
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||||
|
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||||
|
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
|
||||||
|
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
|
||||||
|
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||||
|
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
Loading…
Add table
Add a link
Reference in a new issue