Improvised barcode reader for 1C via Telegram on Go
Hello! I want to share what may be useful to someone.
For a change, I wanted to implement something simple on Go, and here articles on the telegram bots were read at the same time, and at work there was a project on integrating bar coding and 1C, so it was decided to combine business with pleasure and implement an impromptu barcode reader for testing and to feel how in 1C it works.
I decided to use the Telegram bot as a mobile client, they send photos of barcodes (QR codes) to the bot, and it recognizes and sends the whole thing in 1C.
1) We implement the telegram bot, take the first package “ Syfaro / telegram-bot-api ” for working with the telegram api, and for barcode recognition “ barcode.v0" In general, the implementation is simple, you can take a piece when from the example on github - here the bot is ready.
Here is a piece of code that is responsible for working with the image:
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 through com is a “sequence of information + CR”
(both sequences of barcode digits and complex strings encrypted in QR are accepted, but do not forget about the line feed character)
On the other hand, I used com0com to connect eth2com to 1C. 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 HK scanner, specify COM4 and the exchange rate, the line feed character, you're done! To play, you can take the 1C conf “Library of connected equipment”.
PS The implementation of the kneecap without processing the nuances, article one, please do not kick much. Thanks!
For a change, I wanted to implement something simple on Go, and here articles on the telegram bots were read at the same time, and at work there was a project on integrating bar coding and 1C, so it was decided to combine business with pleasure and implement an impromptu barcode reader for testing and to feel how in 1C it works.
I decided to use the Telegram bot as a mobile client, they send photos of barcodes (QR codes) to the bot, and it recognizes and sends the whole thing in 1C.
1) We implement the telegram bot, take the first package “ Syfaro / telegram-bot-api ” for working with the telegram api, and for barcode recognition “ barcode.v0" In general, the implementation is simple, you can take a piece when from the example on github - here the bot is ready.
Here is 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 through com is a “sequence of information + CR”
(both sequences of barcode digits and complex strings encrypted in QR are accepted, but do not forget about the line feed 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, I used com0com to connect eth2com to 1C. 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 HK scanner, specify COM4 and the exchange rate, the line feed character, you're done! To play, you can take the 1C conf “Library of connected equipment”.
PS The implementation of the kneecap without processing the nuances, article one, please do not kick much. Thanks!