You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright © 2023 OpenIM open source community. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package api
  15. import (
  16. "github.com/gin-gonic/gin"
  17. "github.com/openimsdk/openkf/server/internal/common"
  18. "github.com/openimsdk/openkf/server/internal/common/response"
  19. requestparams "github.com/openimsdk/openkf/server/internal/params/request"
  20. "github.com/openimsdk/openkf/server/internal/service"
  21. "github.com/openimsdk/openkf/server/internal/utils"
  22. "github.com/openimsdk/openkf/server/pkg/log"
  23. )
  24. // CreateBot
  25. // @Tags bot
  26. // @Summary CreateBot
  27. // @Description Create a new bot
  28. // @Produce application/json
  29. // @Security ApiKeyAuth
  30. // @Param data body requestparams.CreateBotParams true "CreateBotParams"
  31. // @Success 200 {object} response.Response{msg=string} "Success"
  32. // @Router /api/v1/admin/bot/create [post].
  33. func CreateBot(c *gin.Context) {
  34. uuid, err := utils.GetCommunityUUID(c)
  35. if err != nil {
  36. response.FailWithCode(common.UNAUTHORIZED, c)
  37. return
  38. }
  39. var params requestparams.CreateBotParams
  40. err = c.ShouldBindJSON(&params)
  41. if err != nil {
  42. response.FailWithCode(common.INVALID_PARAMS, c)
  43. return
  44. }
  45. svc := service.NewBotService(c)
  46. _, _, err = svc.CreateBot(uuid, &params)
  47. if err != nil {
  48. response.FailWithAll(common.ERROR, err.Error(), c)
  49. return
  50. }
  51. response.Success(c)
  52. }
  53. // ListBot
  54. // @Tags bot
  55. // @Summary ListBot
  56. // @Description get community bot info
  57. // @Security ApiKeyAuth
  58. // Param data body param.ListPageParams true "ListPageParams"
  59. // @Success 200 {object} response.Response{msg=string} "Success"
  60. // @Router /api/v1/admin/bot/list [post].
  61. func ListBot(c *gin.Context) {
  62. uuid, err := utils.GetCommunityUUID(c)
  63. if err != nil {
  64. response.FailWithCode(common.UNAUTHORIZED, c)
  65. return
  66. }
  67. svc := service.NewBotService(c)
  68. var params requestparams.ListPageParams
  69. err = c.ShouldBindJSON(&params)
  70. if err != nil {
  71. response.FailWithCode(common.INVALID_PARAMS, c)
  72. return
  73. }
  74. resp, err := svc.GetCommunityBotList(uuid, &params)
  75. if err != nil {
  76. response.FailWithCode(common.ERROR, c)
  77. return
  78. }
  79. response.SuccessWithData(resp, c)
  80. }
  81. // DeleteBot
  82. // @Tags bot
  83. // @Summary DeleteBot
  84. // @Description delete bot
  85. // @Security ApiKeyAuth
  86. // Param data body param.DeleteBotParams true "DeleteBotParams"
  87. // @Success 200 {object} response.Response{msg=string} "Success"
  88. // @Router /api/v1/admin/bot/delete [post].
  89. func DeleteBot(c *gin.Context) {
  90. var params requestparams.DeleteBotParams
  91. err := c.ShouldBindJSON(&params)
  92. if err != nil {
  93. response.FailWithCode(common.INVALID_PARAMS, c)
  94. return
  95. }
  96. svc := service.NewBotService(c)
  97. err = svc.DeleteBot(params.UUID)
  98. if err != nil {
  99. log.Debug("DeleteBot error", err)
  100. response.FailWithCode(common.ERROR, c)
  101. return
  102. }
  103. response.Success(c)
  104. }
  105. // UpdateBot
  106. // @Tags bot
  107. // @Summary UpdateBot
  108. // @Description update bot info
  109. // @Security ApiKeyAuth
  110. // Param data body param.UpdateBotParams true "UpdateBotParams"
  111. // @Success 200 {object} response.Response{msg=string} "Success"
  112. // @Router /api/v1/admin/bot/update [post].
  113. func UpdateBot(c *gin.Context) {
  114. var params requestparams.UpdateBotParams
  115. err := c.ShouldBindJSON(&params)
  116. if err != nil {
  117. response.FailWithCode(common.INVALID_PARAMS, c)
  118. return
  119. }
  120. svc := service.NewBotService(c)
  121. info, err := svc.UpdateBotInfo(&params)
  122. if err != nil {
  123. response.FailWithCode(common.ERROR, c)
  124. return
  125. }
  126. response.SuccessWithData(info, c)
  127. }