Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "io/ioutil"
- "log"
- "net/http"
- "strconv"
- //"os"
- "html/template"
- _ "github.com/lib/pq"
- "github.com/dchest/uniuri"
- "database/sql"
- "time"
- //"strings"
- )
- //type UpdateCfg strcut {
- // Timeout integer
- // Offset integer
- //}
- type Update struct {
- //Ok bool `json:"ok"`
- //Result []struct {
- UpdateId int `json:"update_id"`
- Message struct {
- MessageId int `json:"message_id"`
- From struct {
- Id int `json:"id"`
- FirstName string `json:"first_name"`
- LastName string `json:"last_name"`
- LanguageCode string `json:"language_code"`
- } `json:"from"`
- Chat struct {
- Id int `json:"id"`
- FirstName string `json:"first_name"`
- LastName string `json:"last_name"`
- ChatType string `json:"type"`
- } `json:"chat"`
- Date int `json:"date"`
- Text string `json:"text"`
- } `json:"message"`
- } //`json:"result"`
- //}
- type Message struct {
- Ok bool `json:"ok"`
- Result struct {
- MessageId int `json:"message_id"`
- From struct {
- Id int `json:"id"`
- FirstName string `json:"first_name"`
- Username string `json:"username"`
- } `json:"from"`
- Chat struct {
- Id int `json:"id"`
- FirstName string `json:"first_name"`
- LastName string `json:"last_name"`
- ChatType string `json:"type"`
- } `json:"chat"`
- Date int `json:"date"`
- Text string `json:"text"`
- } `json:"result"`
- }
- type Orders []struct {
- OrderId int `json:"order_id"`
- TypeId int `json:"type_id"`
- LocationId int `json:"location_id"`
- VolumeTotal int `json:"volume_total"`
- VolumeRemain int `json:"volume_remain"`
- MinVolume int `json:"min_volume"`
- Price float64 `json:"price"`
- IsBuyOrder bool `json:"is_buy_order"`
- Duration int `json:"duration"`
- Issued string `json:"issued"`
- Range string `json:"range"`
- }
- const (
- Token string = "420535307:AAG8-yNLWdRK3s2kDQ5KzjLYiRowUMP-avI"
- UrlApiTelegram string = "https://api.telegram.org/bot"
- UrlApiEve string = "https://esi.tech.ccp.is/latest"
- WebHookUrl string = "https://aqueous-chamber-20779.herokuapp.com/"
- )
- /*
- func getCharacterOrders(character_id int) Orders {
- query := "/characters/" + strconv.Itoa(character_id) + "/orders/"
- c := http.Client{}
- resp, err := c.Get(UrlApiEve + query)
- if err != nil {
- log.Println(err)
- }
- body, _ := ioutil.ReadAll(resp.Body)
- //fmt.Println(string(body))
- orders := Orders{}
- err = json.Unmarshal(body, &order)
- if err != nil {
- log.Println(err)
- }
- //fmt.Println(order)
- resp.Body.Close()
- return orders
- }
- func getRegionOrders(region_id int) Orders {
- query := "/markets/" + strconv.Itoa(region_id) + "/orders/"
- c := http.Client{}
- resp, err := c.Get(UrlApiEve + query)
- if err != nil {
- log.Println(err)
- }
- body, _ := ioutil.ReadAll(resp.Body)
- //fmt.Println(string(body))
- orders := Orders{}
- err = json.Unmarshal(body, &orders)
- if err != nil {
- log.Println(err)
- }
- //fmt.Println(order)
- resp.Body.Close()
- return orders
- }
- func getUpdate(timeout int, offset int) Update {
- path := UrlApiTelegram + Token
- query := "/getUpdates?timeout="
- c := http.Client{}
- resp, err := c.Get(path + query + strconv.Itoa(timeout) + "&offset=" + strconv.Itoa(offset))
- if err != nil {
- log.Println(err)
- }
- body, _ := ioutil.ReadAll(resp.Body)
- //fmt.Println(string(body)
- //decoder := json.NewDecoder(resp.Body)
- //var update interface{}
- update := Update{}
- //err = decoder.Decode(&update)
- err = json.Unmarshal(body, &update)
- if err != nil {
- log.Println(err)
- }
- resp.Body.Close()
- return update
- }
- */
- func SetWebhook() {
- path := UrlApiTelegram + Token
- query := "/setWebhook?url=" + WebHookUrl
- c := http.Client{}
- resp, err := c.Get(path + query)
- if err != nil {
- log.Println(err)
- }
- body, _ := ioutil.ReadAll(resp.Body)
- log.Println(string(body))
- resp.Body.Close()
- }
- func sendMessage(chat_id int, text string, reply_to_message_id int) Message {
- path := UrlApiTelegram + Token
- query := "/sendMessage?chat_id="
- c := http.Client{}
- resp, err := c.Get(path + query + strconv.Itoa(chat_id) + "&text=" + text + "&reply_to_message_id=" + strconv.Itoa(reply_to_message_id))
- if err != nil {
- log.Println(err)
- }
- body, _ := ioutil.ReadAll(resp.Body)
- message := Message{}
- err = json.Unmarshal(body, &message)
- if err != nil {
- log.Println(err)
- }
- resp.Body.Close()
- return message
- }
- /*func mapface(m interface{}, mm map[string]interface{}) map[string]interface{} {
- for k, v := range m.(map[string]interface{}) {
- switch vv := v.(type) {
- case string:
- mm[k] = v
- fmt.Println(k)
- case []interface{}:
- //vv[0] = vv[0].([]interface{})
- return mapface(vv[0], mm)
- }
- }
- //fmt.Println(mm)
- return mm
- }*/
- func main() {
- var sessions = map[string]string{}
- SetWebhook()
- //port := os.Getenv("PORT")
- port := "8085"
- //handler1 := HandlerWebhook{}
- //handler2 := HandlerIndex{}
- http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- if r.Method == http.MethodPost {
- body, _ := ioutil.ReadAll(r.Body)
- log.Println("Body: ", string(body))
- update := Update{}
- err := json.Unmarshal(body, &update)
- if err != nil {
- log.Println(err)
- }
- /*switch update.Message.Text {
- case "":
- case "":
- default:
- sendMessage(update.Message.Chat.Id, update.Message.Text, 0)
- }
- fmt.Println(update.Message.Text)
- message := sendMessage(update.Message.Chat.Id, update.Message.Text, 0)
- if message.Result.Text != update.Message.Text {
- log.Println(message)
- }
- /*for _, v := range update.Result {
- fmt.Println(v.Message.Text)
- message := sendMessage(v.Message.Chat.Id, v.Message.Text, 0)
- if message.Result.Text != v.Message.Text {
- log.Println(message)
- }
- }*/
- r.Body.Close()
- }
- })
- http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
- /*rows, err := db.Query("SELECT * FROM Tasks")
- for rows.Next() {
- var id int
- var text, solution string
- err = rows.Scan(&id, &text, &solution)
- if err != nil {
- log.Println(err)
- } else {
- fmt.Println(strconv.Itoa(id) + " " + text + " " + solution)
- }
- }*/
- sessionID, err := r.Cookie("session_id")
- if err == http.ErrNoCookie {
- http.Redirect(w, r, "/login", 301)
- return
- } else if err != nil {
- log.Println(err)
- }
- fmt.Println(sessionID)
- _, ok := sessions[sessionID.Value]
- if !ok {
- fmt.Fprint(w, "Session not found")
- //http.Redirect(w, r, "/login", 301)
- } else {
- if r.Method == http.MethodPost {
- r.ParseForm()
- db, err := sql.Open("postgres", "user=irina_dashevskaya dbname=mylocaldb sslmode=disable")
- if err != nil {
- log.Println(err)
- }
- if _, err = db.Exec("INSERT INTO Tasks (Text, Solution) VALUES ($1, $2)", r.Form["task"][0], r.Form["solution"][0]); err != nil {
- log.Println(err)
- }
- return
- }
- tmpl, err := template.ParseFiles("add.html")
- if err != nil {
- log.Println("Can not expand template", err)
- return
- }
- if err = tmpl.Execute(w, nil); err != nil {
- log.Println(err)
- }
- }
- })
- http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
- if r.Method == http.MethodPost {
- var login string
- r.ParseForm()
- db, err := sql.Open("postgres", "user=irina_dashevskaya dbname=mylocaldb sslmode=disable")
- if err != nil {
- log.Println(err)
- }
- err = db.QueryRow("SELECT login FROM Users WHERE login = $1 AND password = $2", r.Form["login"][0], r.Form["password"][0]).Scan(&login)
- if err != nil {
- log.Println(err)
- }
- if login == "" {
- fmt.Println("Incorrect username or password")
- } else {
- sessionID := uniuri.NewLen(32)
- sessions[sessionID] = login
- expiration := time.Now().Add(365 * 24 * time.Hour)
- cookie := http.Cookie{Name: "session_id", Value: sessionID, Expires: expiration}
- http.SetCookie(w, &cookie)
- fmt.Println("Cookie is set")
- http.Redirect(w, r, "/add", 301)
- return
- }
- } else {
- tmpl, err := template.ParseFiles("template.html")
- if err != nil {
- log.Println("Can not expand template", err)
- return
- }
- if err = tmpl.Execute(w, nil); err != nil {
- log.Println(err)
- }
- }
- /*db, err := sql.Open("postgres", "user=irina_dashevskaya dbname=mylocaldb sslmode=disable")
- if err != nil {
- log.Println(err)
- }
- rows, err := db.Query("SELECT * FROM Tasks")
- for rows.Next() {
- var id int
- var text, solution string
- err = rows.Scan(&id, &text, &solution)
- if err != nil {
- log.Println(err)
- } else {
- fmt.Println(strconv.Itoa(id) + " " + text + " " + solution)
- }
- }
- tmpl, err := template.ParseFiles("template.html")
- if err != nil {
- log.Println("Can not expand template", err)
- return
- }
- err = tmpl.Execute(w, nil)
- if err != nil {
- log.Println(err)
- }*/
- } )
- http.ListenAndServe(":"+port, nil)
- return
- /*
- timeout := 5
- offset := 0
- //orders := getRegionOrders(10000001)
- //fmt.Println(len(order))
- for {
- update := getUpdate(timeout, offset)
- if len(update.Result) == 0 {
- continue
- }
- offset = update.Result[0].UpdateId + 1
- for _, v := range update.Result {
- fmt.Println(v.Message.Text)
- message := sendMessage(v.Message.Chat.Id, v.Message.Text, 0)
- if message.Result.Text != v.Message.Text {
- log.Println(message)
- }
- }
- }*/
- //m := update.(map[string]interface{})
- //mm := make(map[string]interface{})
- //mm = mapface(m, mm)
- //result := m["result"].([]interface{})[0]
- //fmt.Println(mm)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement