R
R
re-incarnation2021-06-25 15:16:26
API
re-incarnation, 2021-06-25 15:16:26

The problem with gluing lines, where is the error in the code?

code
package main

import (
  "context"
  "log"
  "fmt"
  "strings"
  "strconv"
  "database/sql"

_ "github.com/go-sql-driver/mysql"
  "github.com/SevereCloud/vksdk/v2/api"
  "github.com/SevereCloud/vksdk/v2/events"
  "github.com/SevereCloud/vksdk/v2/longpoll-bot"
  "github.com/davecgh/go-spew/spew"
)

const token = "***"
var vk = api.NewVK(token)
var str strings.Builder
var ucp Ucp

type Ucp struct {
  Id int
  Text string
  Closed uint16
  Asker int
  Answer int
}

var help_text = "Для того что бы задать вопрос асистентам, напишите /ask и ваш вопрос."
var help_text2 = "Ваш вопрос отправлен в поддержку, ожидайте ответа оператора."

func main() {
  group, err := vk.GroupsGetByID(nil)
  if err != nil {
    log.Fatal(err)
  }

  lp, err := longpoll.NewLongPoll(vk, group[0].ID)
  if err != nil {
    log.Fatal(err)
  }

  //help
  lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
      /* полезная вещь */	log.Printf("%d: %s", obj.Message.FromID, obj.Message.Text)
    if obj.Message.Text == "/help" {
      send, err := vk.MessagesSend(api.Params{
        "peer_id": obj.Message.FromID,
        "random_id": 0,
        "message": help_text,
      })

      if err != nil {
        log.Fatal(err)
      }
      spew.Dump(send)
    }
  })

  //start ask + resend msg and insert
  lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
    msg := obj.Message.Text
    asker_id := obj.Message.FromID
    //prefix
    startWith := "/ask"
    //cheack prefix
    starts := strings.HasPrefix(msg, startWith)
    if starts == true {
      //trim message
      res_trim := strings.Trim(msg, "/ask")
      //cheack empty text
      if res_trim != "" {
      //cheack where send message
      if obj.Message.PeerID != 2000000001 {
      //send message
      ucp_send_beseda, err11 := vk.MessagesSend(api.Params{
        "peer_id": obj.Message.FromID,
        "random_id": 0,
        "message": help_text2,
      })
      //err cheack
      if err11 != nil {
        panic(err11)
      }

      //db connect
      db, err12 := sql.Open("mysql", "login:[email protected](ip:port)/db")
      if err12 != nil {
        panic(err)
      }

      //insert ask to bd
      insert, err13 := db.Query(fmt.Sprintf("INSERT INTO `ucp` (`text`, `asker`) VALUES('%s', '%d')", res_trim, asker_id))
      if err13 != nil {
        panic(err13)
      }

      //select ask and send to beseda
      res_select_ask, err14 := db.Query(fmt.Sprintf("SELECT `id`, `text` FROM `ucp` WHERE `text` = '%s'", res_trim))
      if err14 != nil {
        panic(err14)
      }

      //take id and text from req
      for res_select_ask.Next(){
        err15 := res_select_ask.Scan(&ucp.Id, &ucp.Text)
        if err15 != nil {
          panic(err15)
        }
          str.WriteString("NEW QUESTION:\r\nID- " + strconv.Itoa(ucp.Id) + " TEXT- " + ucp.Text)
      }

      //send message to beseda
      ucp_send_beseda_assistant, err16 := vk.MessagesSend(api.Params{
        "peer_id": 2000000001,
        "random_id": 0,
        "message": str.String(),
      })
      if err16 != nil {
        panic(err)
      }
    //dumps
    spew.Dump(ucp_send_beseda)
    spew.Dump(insert)
    spew.Dump(res_select_ask)
    spew.Dump(ucp_send_beseda_assistant)
    }
  }
  }
  })

    //list command
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
  if obj.Message.Text == "/list" {
    if obj.Message.PeerID == 2000000001{
      //db connect
      db, err12 := sql.Open("mysql", "login:[email protected](ip:port)/db")
      if err12 != nil {
        panic(err12)
      }

      //select asks and send to beseda
      res_select_list, err17 := db.Query("SELECT `id`, `text` FROM `ucp` WHERE `closed` = '0' AND `answer` = '0'")
      if err17 != nil {
        panic(err17)
      }

      //take id and text from req
      for res_select_list.Next(){
        err18 := res_select_list.Scan(&ucp.Id, &ucp.Text)
        if err18 != nil {
          panic(err18)
        }
          str.WriteString("LIST QUESTION:\r\nID- " + strconv.Itoa(ucp.Id) + " TEXT- " + ucp.Text)
      }

    send_ask_list, err19 := vk.MessagesSend(api.Params{
      "peer_id": 2000000001,
      "random_id": 0,
      "message": str.String(),
    })

    if err19 != nil {
      send_ask_list_error, err20 := vk.MessagesSend(api.Params{
        "peer_id": 2000000001,
        "random_id": 0,
        "message": "Empty",
      })
      if err20 != nil {
        panic(err20)
      }
            spew.Dump(send_ask_list_error)
    }

    spew.Dump(send_ask_list)

    }
  }
})
  // Run Bots Long Poll
  log.Println("Start Long Poll")
  if err := lp.Run(); err != nil {
    log.Fatal(err)
  }
}

The problem is that when I write /ask to the bot, it sends this message to the conversation, but it sends it very crookedly. The plan is crooked - first it sends the entire list of questions that I have to do through the /list command, and after that this message about a new question, and several times.
Well, if you send /list, then for some reason it sends the entire list first, after a message about a new question, and then again the list.
PS concatenation of strings "works", it combines these strings and displays them in one message, but it displays them incorrectly...

UPD2: I found a solution to the problem with infinite multiplication of strings, it was necessary to clear the buffer after sending the message ( str.Reset() ), but the problem remains - /list crookedly sends lines.
code/list
//list command
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
  if obj.Message.Text == "/list" {
    if obj.Message.PeerID == 2000000001{
      //db connect
      db, err12 := sql.Open("mysql", "login:[email protected](ip:port)/db")
      if err12 != nil {
        panic(err12)
      }

      //select asks and send to beseda
      res_select_list, err17 := db.Query("SELECT `id`, `text` FROM `ucp` WHERE `closed` = '0' AND `answer` = '0'")
      if err17 != nil {
        panic(err17)
      }

      //take id and text from req
      for res_select_list.Next(){
        err18 := res_select_list.Scan(&ucp.Id, &ucp.Text)
        if err18 != nil {
          panic(err18)
        }
          str.WriteString("LIST QUESTION:\r\nID- " + strconv.Itoa(ucp.Id) + " TEXT- " + ucp.Text)
      }

    send_ask_list, err19 := vk.MessagesSend(api.Params{
      "peer_id": 2000000001,
      "random_id": 0,
      "message": str.String(),
    })
            str.Reset()

    if err19 != nil {
      send_ask_list_error, err20 := vk.MessagesSend(api.Params{
        "peer_id": 2000000001,
        "random_id": 0,
        "message": "Empty",
      })
      if err20 != nil {
        panic(err20)
      }
            spew.Dump(send_ask_list_error)
    }

    spew.Dump(send_ask_list)

    }
  }
})


what the sheet sends
LIST QUESTION:
ID- 19 TEXT- testLIST QUESTION:
ID- 20 TEXT- hdjsksbLIST QUESTION:
ID- 21 TEXT- jsjsjLIST QUESTION:
ID- 22 TEXT- teteteLIST QUESTION:
ID- 23 TEXT- teteteLIST QUESTION:
ID- 24 TEXT- sdasdLIST QUESTION:
ID- 25 TEXT- teLIST QUESTION:
ID- 26 TEXT- LIST QUESTION:
ID- 27 TEXT- teasihidoah

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-06-25
@re-incarnation

You need to edit this block of code

for res_select_list.Next(){
    err18 := res_select_list.Scan(&ucp.Id, &ucp.Text)
    if err18 != nil {
        panic(err18)
    }
    str.WriteString("LIST QUESTION:\r\nID- " + strconv.Itoa(ucp.Id) + " TEXT- " + ucp.Text)
}

Do like this
str.WriteString("LIST QUESTION:\r\n")
for res_select_list.Next(){
    err18 := res_select_list.Scan(&ucp.Id, &ucp.Text)
    if err18 != nil {
        panic(err18)
    }
    str.WriteString("ID- " + strconv.Itoa(ucp.Id) + " TEXT- " + ucp.Text + "\r\n")
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question