Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.69 KB | None | 0 0
  1. type Action struct {
  2.     Type    string
  3.     Payload string
  4.     Label   string
  5. }
  6.  
  7. type Button struct {
  8.     Action  Action
  9.     Color   string
  10. }
  11.  
  12. type Buttons struct {
  13.     Buttons []Button
  14. }
  15.  
  16. func InitBByte(label string, payload []byte, color string) Button {
  17.  
  18.     b := Button{
  19.         Action: Action{
  20.             Type:    "text",
  21.             Payload: string(payload),
  22.             Label:   template.URLQueryEscaper(label),
  23.         },
  24.         Color: color,
  25.     }
  26.     return b
  27. }
  28.  
  29.  
  30. func Keyboard(chat float64, args ...Buttons) vk.H {
  31.  
  32.     var bs1 []Button
  33.     var bs2 []Button
  34.     var bs3 []Button
  35.     var bs4 []Button
  36.  
  37.     for k, v := range args {
  38.         if k == 0 {
  39.             for _, b := range v.Buttons {
  40.                 bs1 = append(bs1, b)
  41.             }
  42.         }
  43.         if k == 1 {
  44.             for _, b := range v.Buttons {
  45.                 bs2 = append(bs2, b)
  46.             }
  47.         }
  48.         if k == 2 {
  49.             for _, b := range v.Buttons {
  50.                 bs3 = append(bs3, b)
  51.             }
  52.         }
  53.         if k == 3 {
  54.             for _, b := range v.Buttons {
  55.                 bs4 = append(bs4, b)
  56.             }
  57.         }
  58.     }
  59.    
  60.     params := vk.H{}
  61.     keb := vk.H{}
  62.     if int(chat) > 2000000000 {
  63.         keb["inline"] = true
  64.     }
  65.  
  66.     keb["one_time"] = false
  67.     if bs2 == nil {
  68.         keb["buttons"] = []interface{}{bs1}
  69.     } else if bs3 == nil {
  70.         keb["buttons"] = []interface{}{bs1, bs2}
  71.     } else if bs4 == nil {
  72.         keb["buttons"] = []interface{}{bs1, bs2, bs3}
  73.     } else {
  74.         keb["buttons"] = []interface{}{bs1, bs2, bs3, bs4}
  75.     }
  76.  
  77.  
  78.     kbj, _ := json.Marshal(keb)
  79.  
  80.     params["keyboard"] = strings.ToLower(string(kbj))
  81.  
  82.     return params
  83. }
  84.  
  85.  
  86. func KbInline(one, two, three Buttons) vk.H {
  87.  
  88.     var bs1 []Button
  89.     var bs2 []Button
  90.     var bs3 []Button
  91.  
  92.     for _, b := range one.Buttons {
  93.         bs1 = append(bs1, b)
  94.     }
  95.  
  96.     for _, b := range two.Buttons {
  97.         bs2 = append(bs2, b)
  98.     }
  99.  
  100.     for _, b := range three.Buttons {
  101.         bs3 = append(bs3, b)
  102.     }
  103.  
  104.  
  105.     params := vk.H{}
  106.     kb := vk.H{}
  107.  
  108.     kb["inline"] = true
  109.     if bs2 == nil {
  110.         kb["buttons"] = []interface{}{one.Buttons}
  111.     } else if bs3 == nil {
  112.         kb["buttons"] = []interface{}{bs1, bs2}
  113.     } else {
  114.         kb["buttons"] = []interface{}{bs1, bs2, bs3}
  115.     }
  116.  
  117.     kbj, _ := json.Marshal(kb)
  118.  
  119.     params["keyboard"] = strings.ToLower(string(kbj))
  120.  
  121.     return params
  122. }
  123.  
  124.  
  125.  
  126. func Back(payload string) Buttons {
  127.     s := InitBByte("Назад", []byte(`{"payload":"`+payload+`"}`), "secondary")
  128.     params := Buttons{Buttons: []Button{s}}
  129.     return params
  130. }
  131.  
  132.  
  133. func Menu(i float64) Buttons {
  134.     if i < 2000000000 {
  135.         s := InitBByte("Назад", []byte(`{"payload":"menu"}`), "secondary")
  136.         params := Buttons{Buttons: []Button{s}}
  137.         return params
  138.     } else {
  139.         return Buttons{}
  140.     }
  141. }
  142.  
  143. ##########################################
  144. s := bts.InitBByte("Моя стата", []byte(`{"payload":"invitestat"}`), "primary")
  145.     params := bts.Keyboard(id.Chat, bts.Buttons{Buttons: []bts.Button{s}}, bts.Menu(id.Chat))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement