diff --git a/internal/transport/vk/api.go b/internal/transport/vk/api.go deleted file mode 100644 index 407c225..0000000 --- a/internal/transport/vk/api.go +++ /dev/null @@ -1,40 +0,0 @@ -package vk - -import ( - "io/ioutil" - "net/http" - "strings" -) - -const ( - apiURL = "https://api.vk.com/method/" - apiVersion = "5.52" -) - -// CallMethod ... -func (c *Client) CallMethod(method string, p RequestParams) ([]byte, error) { - params, err := p.URLValues() - if err != nil { - return nil, err - } - - params.Set("access_token", c.accessToken) - - if params.Get("v") == "" { - params.Set("v", apiVersion) - } - - req, err := http.NewRequest(http.MethodPost, apiURL+method, strings.NewReader(params.Encode())) - if err != nil { - return nil, err - } - - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - resp, err := c.client.Do(req) - if err != nil { - return nil, err - } - - return ioutil.ReadAll(resp.Body) -} diff --git a/internal/transport/vk/client.go b/internal/transport/vk/client.go deleted file mode 100644 index eb46893..0000000 --- a/internal/transport/vk/client.go +++ /dev/null @@ -1,88 +0,0 @@ -package vk - -import ( - "chat-transport/internal/entities" - "errors" - "fmt" - "net/http" -) - -// Client ... -type Client struct { - name string - accessToken string - chatID string - ignoreAccounts []string - lastUpdateID int - client *http.Client -} - -// NewClient ... -func NewClient(name, accessToken, chatID string, ignoreAccounts []string) *Client { - return &Client{ - name: name, - accessToken: accessToken, - chatID: chatID, - ignoreAccounts: ignoreAccounts, - client: &http.Client{}, - } -} - -// GetName ... -func (c *Client) GetName() string { - return c.name -} - -// GetChatID ... -func (c *Client) GetChatID() string { - return c.chatID -} - -// Validate ... -func (c *Client) Validate() error { - if c.accessToken == "" { - return errors.New("access_token value not valid") - } - - if c.chatID == "" { - return errors.New("chat value not valid") - } - - return nil -} - -// GetNewMessages ... -func (c *Client) GetNewMessages() ([]*entities.Message, error) { - // https://vk.com/dev/messages.get - p := RequestParams{ - "offset": 1, - "time_offset": "", - "last_message_id": 0, - } - data, err := c.CallMethod("messages.get", p) - if err != nil { - return nil, err - } - fmt.Printf("%s", data) - - return nil, nil -} - -// SendMessage ... -func (c *Client) SendMessage(m *entities.Message) error { - // https://vk.com/dev/messages.send - p := RequestParams{ - "user_id": c.chatID, - "peer_id": c.chatID, - "chat_id": c.chatID, - "message": m.Text, - } - data, err := c.CallMethod("messages.send", p) - if err != nil { - return err - } - - fmt.Printf("%s", data) - - return nil -} diff --git a/internal/transport/vk/entities.go b/internal/transport/vk/entities.go deleted file mode 100644 index 21757ed..0000000 --- a/internal/transport/vk/entities.go +++ /dev/null @@ -1,35 +0,0 @@ -package vk - -import ( - "fmt" - "net/url" -) - -type responseMessage struct { - Response struct { - Count int - Items []struct { - ID int - Date int - Out int - UserID int - ReadState int - Title string - Body string - } - } -} - -// RequestParams ... -type RequestParams map[string]interface{} - -// URLValues ... -func (p RequestParams) URLValues() (url.Values, error) { - values := url.Values{} - - for k, v := range p { - values.Add(k, fmt.Sprint(v)) - } - - return values, nil -}