Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions example/telegram/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func usage() {
fmt.Print(" auth <phone_number> auth connection by code\n")
fmt.Print(" msg <user_id> <msgtext> send message to user\n")
fmt.Print(" list get contact list\n")
fmt.Print(" contacts_search search contact by pattern\n")
fmt.Println()
}

Expand All @@ -26,7 +27,7 @@ func main() {
os.Exit(1)
}

commands := map[string]int{"auth": 1, "msg": 2, "list": 0}
commands := map[string]int{"auth": 1, "msg": 2, "list": 0, "contacts_search": 2}
valid := false
for k, v := range commands {
if os.Args[1] == k {
Expand Down Expand Up @@ -62,9 +63,12 @@ func main() {
case "msg":
user_id, _ := strconv.Atoi(os.Args[2])
err = m.SendMsg(int32(user_id), os.Args[3])

case "list":
err = m.GetContacts()
case "contacts_search":
pattern := string(os.Args[2])
limit, _ := strconv.Atoi(os.Args[3])
err = m.SearchContacts(pattern, int32(limit))
}

if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,33 @@ func (m *MTProto) GetContacts() error {
return nil
}

func (m *MTProto) SearchContacts(q string, limit int32) error {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{TL_contacts_search{q, limit}, resp}
x := <-resp
list, ok := x.(TL_contacts_found)
if !ok {
return fmt.Errorf("RPC: %#v", x)
}

contacts := make(map[int32]TL_userContact)
for _, v := range list.users {
if v, ok := v.(TL_userContact); ok {
contacts[v.id] = v
}
}

for _, v := range list.users {
v := v.(TL_userForeign)
fmt.Printf(
"%-20s %30s\n",
"@" + v.username,
v.first_name)
}

return nil
}

func (m *MTProto) SendMsg(user_id int32, msg string) error {
resp := make(chan TL, 1)
m.queueSend <- packetToSend{
Expand Down