Advertisement
Guest User

Untitled

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