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:password@tcp(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:password@tcp(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)
}
}