📜 ⬆️ ⬇️

Improvised barcode reader for 1C via Telegram on Go

Hello! I want to share what might be useful to someone.

I wanted, for a change, something simple to implement on Go, and here in parallel were read articles on the Habré about Telegram bots, and at work a project on the integration of bar-coding and 1C, well, so it was decided to combine business with pleasure and implement an improvised barcode reader for testing and on touch as in 1C it works.

I decided to use the Telegram bot as a mobile client, the bot sends a photo of bar codes (QR codes), and he recognizes and sends the whole thing in 1C.
')
1) Implement the telegram bot, take the first available package “ Syfaro / telegram-bot-api ” to work with the api telegram, and for barcode recognition “ barcode.v0 ”. In general, the implementation is simple, you can take a piece when from the example on github - this is the bot and ready.

I will give a piece of code that is responsible for working with the image:

for update:=range updates { if update.Message == nil { continue } log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) cmd:=update.Message.Text if update.Message.Photo!=nil{ //  ,       photo :=*update.Message.Photo log.Print(photo[1].FileID)//  2    ,  ,   resp, err :=bot.GetFile(tgbotapi.FileConfig{photo[1].FileID}) r, err := http.Get("https://api.telegram.org/file/bot"+telegram_token+"/"+resp.FilePath) //    telegram if err!=nil{ log.Print(err) } defer r.Body.Close() src, _ := jpeg.Decode(r.Body) // io.Reader  image.Image img:=barcode.NewImage(src) scanner := barcode.NewScanner().SetEnabledAll(true) re, err := scanner.ScanImage(img) //     if err!=nil{ log.Print(err) } for _, s := range re { fmt.Println(s.Type.Name(), s.Data) msg := tgbotapi.NewMessage(update.Message.Chat.ID, s.Data) msg.ReplyToMessageID = update.Message.MessageID bot.Send(msg) //            1 (   eth2com,   1     ) r, err := http.Get("http://192.168.0.2:7070/"+s.Data) //   if err!=nil{ log.Print(err) } defer r.Body.Close() } } 

2) Now let's move on to eth2com (not to be confused with a similar program) here we use this and this .

Actually, the whole exchange with 1C via com is “the sequence of information + CR”
(both sequences of digits of the barcode and complex strings encrypted in QR are accepted, but do not forget about the newline character)

 func main(){ e:=echo.New() c := &serial.Config{Name: "COM3", Baud: 115200} s, err := serial.OpenPort(c) //  COM3 if err != nil { log.Fatal(err) } e.GET("/:code", func(c echo.Context) error { // GET   ,        log.Print(c.Param("code")) _, err := s.Write([]byte(c.Param("code")+"\r"))//  COM    \r  1   if err != nil { log.Fatal(err) } return c.String(http.StatusOK,c.Param("code")) }) e.Logger.Fatal(e.Start(":7070")) } 

On the other hand, for the eth2com connection with 1C, I used the com0com program. As a result, such a scheme came out:

User -> Telegram bot -> eth2com -> COM3 -> (com0com) -> COM4-> 1C.

From the 1C side, everything is quite simple: in the settings of the SK scanner, we specify COM4 and the exchange rate, a newline character, that's it! To play you can take konfu 1C "Library of the connected equipment."

PS Implementation knee without the processing of nuances, the first article, please do not kick much. Thank!

Source: https://habr.com/ru/post/335916/


All Articles