icq

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2018 License: MIT Imports: 9 Imported by: 0

README

ICQ Bot API

Installation

Go get: go get gopkg.in/icq.v1

Go mod / Go dep: import "gopkg.in/icq.v1"

Working

Methods:

  • SendMessage
  • UploadFile

Webhooks to get updates

Example

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	"gopkg.in/icq.v1"
)

func main() {
	// New API object
	b := icq.NewAPI(os.Getenv("ICQ_TOKEN"))

	// Send message
	r, err := b.SendMessage("429950", "Hello, world!")
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(r.State)

	// Send file
	f, err := os.Open("./example/icq.png")
	defer f.Close()
	if err != nil {
		log.Fatalln(err)
	}
	file, err := b.UploadFile("icq.png", f)
	if err != nil {
		log.Fatalln(err)
	}
	b.SendMessage("429950", file)

	// Webhook usage
	updates := make(chan icq.Update)
	errors := make(chan error)
	osSignal := make(chan os.Signal, 1)

	m := http.NewServeMux()
	m.HandleFunc("/webhook", b.GetWebhookHandler(updates, errors)) // Webhook sets here

	h := &http.Server{Addr: ":8080", Handler: m}
	go func() {
		log.Fatalln(h.ListenAndServe())
	}()
	signal.Notify(osSignal, os.Interrupt)
	signal.Notify(osSignal, os.Kill)
	for {
		select {
		case u := <-updates:
			log.Println("Incomming message", u)
			b.SendMessage(u.Update.From.ID, fmt.Sprintf("You sent me: %s", u.Update.Text))
			// ... process ICQ updates ...
		case err := <-errors:
			log.Fatalln(err)
		case sig := <-osSignal:
			ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
			h.Shutdown(ctx)
			log.Fatalln("OS signal:", sig.String())
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	// contains filtered or unexported fields
}

API

func NewAPI

func NewAPI(token string) *API

NewAPI constructor of API object

func (*API) GetWebhookHandler

func (a *API) GetWebhookHandler(cu chan<- Update, e chan<- error) func(w http.ResponseWriter, r *http.Request)

GetWebhookHandler returns http.HandleFunc that parses webhooks

func (*API) SendMessage

func (a *API) SendMessage(to string, message string) (*MessageResponse, error)

SendMessage with `message` text to `to` participant

func (*API) UploadFile

func (a *API) UploadFile(fileName string, r io.Reader) (string, error)

UploadFile to ICQ servers and returns URL to file

type Chat

type Chat struct {
	ID string `json:"id"`
}

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTP Client interface

type MessageResponse

type MessageResponse struct {
	SubCode struct {
		Error int `json:"error"`
	} `json:"subCode"`
	MessageID        string `json:"msgId"`
	HistoryMessageID int64  `json:"histMsgId"`
	State            string `json:"state"`
}

type Response

type Response struct {
	Response struct {
		StatusCode int              `json:"statusCode"`
		StatusText string           `json:"statusText"`
		RequestId  string           `json:"requestId"`
		Data       *MessageResponse `json:"data"`
	} `json:"response"`
}

type Update

type Update struct {
	Update struct {
		Chat Chat   `json:"chat"`
		Date int    `json:"date"`
		From User   `json:"from"`
		Text string `json:"text"`
	} `json:"update"`
	UpdateID int `json:"update_id"`
}

type User

type User struct {
	ID           string `json:"id"`
	LanguageCode string `json:"language_code"`
}

type WebhookRequest

type WebhookRequest struct {
	Token   string   `json:"aimsid"`
	Updates []Update `json:"update"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL