Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
katursis committed Jan 27, 2020
1 parent fcfa1c2 commit 862352f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and use `vk` as the package name inside the code.

## Example

[Full example with errors handling](https://github.com/go-vk-api/vk/blob/master/example/example.go)

```go
package main

Expand Down
40 changes: 26 additions & 14 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,34 @@ func main() {
log.Panic(err)
}

for update := range stream.Updates {
switch data := update.Data.(type) {
case *lp.NewMessage:
if data.Text == "/hello" {
var sentMessageID int64
for {
select {
case update, ok := <-stream.Updates:
if !ok {
return
}

if err = client.CallMethod("messages.send", vk.RequestParams{
"peer_id": data.PeerID,
"message": "Hello!",
"forward_messages": data.ID,
"random_id": 0,
}, &sentMessageID); err != nil {
log.Panic(err)
}
switch data := update.Data.(type) {
case *lp.NewMessage:
if data.Text == "/hello" {
var sentMessageID int64

if err = client.CallMethod("messages.send", vk.RequestParams{
"peer_id": data.PeerID,
"message": "Hello!",
"forward_messages": data.ID,
"random_id": 0,
}, &sentMessageID); err != nil {
log.Panic(err)
}

log.Println(sentMessageID)
log.Println(sentMessageID)
}
}
case err, ok := <-stream.Errors:
if ok {
// stream.Stop()
log.Panic(err)
}
}
}
Expand Down

0 comments on commit 862352f

Please sign in to comment.