mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-26 23:10:42 +02:00
сделал telegram_client.go
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,3 +8,4 @@
|
|||||||
/log.txt
|
/log.txt
|
||||||
/common/cover.out
|
/common/cover.out
|
||||||
/whatsapp.db
|
/whatsapp.db
|
||||||
|
/session.txt
|
||||||
|
|||||||
@@ -232,6 +232,7 @@ func Send_BPMN_File(BPMN_filename string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FileName = dir + FileName
|
FileName = dir + FileName
|
||||||
|
log.Info("Load .bpmn file from: ", FileName)
|
||||||
|
|
||||||
_, err = Client.NewDeployResourceCommand().AddResourceFile(FileName).Send(ctx)
|
_, err = Client.NewDeployResourceCommand().AddResourceFile(FileName).Send(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -204,6 +204,7 @@ func ProgramDir_Common() string {
|
|||||||
substr := "/tmp/"
|
substr := "/tmp/"
|
||||||
pos1 := strings.Index(sdir, substr)
|
pos1 := strings.Index(sdir, substr)
|
||||||
if pos1 >= 0 {
|
if pos1 >= 0 {
|
||||||
|
//linux
|
||||||
filename = CurrentFilename()
|
filename = CurrentFilename()
|
||||||
dir = filepath.Dir(filename)
|
dir = filepath.Dir(filename)
|
||||||
|
|
||||||
@@ -216,22 +217,23 @@ func ProgramDir_Common() string {
|
|||||||
//dir = FindDirUp(dir)
|
//dir = FindDirUp(dir)
|
||||||
//dir = FindDirUp(dir)
|
//dir = FindDirUp(dir)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
//Windows
|
||||||
|
substr = "\\temp\\"
|
||||||
|
pos1 = strings.Index(sdir, substr)
|
||||||
|
if pos1 >= 0 {
|
||||||
|
filename = CurrentFilename()
|
||||||
|
dir = filepath.Dir(filename)
|
||||||
|
|
||||||
substr = "\\temp\\"
|
substr := SeparatorFile() + "vendor" + SeparatorFile()
|
||||||
pos1 = strings.Index(sdir, substr)
|
pos_vendor := strings.Index(strings.ToLower(dir), substr)
|
||||||
if pos1 >= 0 {
|
if pos_vendor >= 0 {
|
||||||
filename = CurrentFilename()
|
dir = dir[0:pos_vendor]
|
||||||
dir = filepath.Dir(filename)
|
} else if dir[len(dir)-5:] == "micro" {
|
||||||
|
dir = FindDirUp(dir)
|
||||||
substr := SeparatorFile() + "vendor" + SeparatorFile()
|
//dir = FindDirUp(dir)
|
||||||
pos_vendor := strings.Index(strings.ToLower(dir), substr)
|
//dir = FindDirUp(dir)
|
||||||
if pos_vendor >= 0 {
|
}
|
||||||
dir = dir[0:pos_vendor]
|
|
||||||
} else if dir[len(dir)-5:] == "micro" {
|
|
||||||
dir = FindDirUp(dir)
|
|
||||||
//dir = FindDirUp(dir)
|
|
||||||
//dir = FindDirUp(dir)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package nats_connect
|
package nats_connect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
"github.com/ManyakRus/starter/logger"
|
"github.com/ManyakRus/starter/logger"
|
||||||
|
"github.com/ManyakRus/starter/micro"
|
||||||
"github.com/ManyakRus/starter/ping"
|
"github.com/ManyakRus/starter/ping"
|
||||||
"os"
|
"os"
|
||||||
//"github.com/ManyakRus/starter/common/v0/micro"
|
//"github.com/ManyakRus/starter/common/v0/micro"
|
||||||
@@ -121,3 +123,24 @@ func FillSettings() {
|
|||||||
|
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendMessageCtx - Отправляет сообщение, учитывает таймаут контекста
|
||||||
|
func SendMessageCtx(ctx context.Context, subject string, data []byte) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
fn := func() error {
|
||||||
|
err = SendMessage(subject, data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = micro.GoGo(ctx, fn)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMessage - Отправляет сообщение
|
||||||
|
func SendMessage(subject string, data []byte) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
err = Conn.Publish(subject, data)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package nats_connect
|
package nats_connect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/ManyakRus/starter/config"
|
"github.com/ManyakRus/starter/config"
|
||||||
"github.com/ManyakRus/starter/contextmain"
|
"github.com/ManyakRus/starter/contextmain"
|
||||||
"github.com/ManyakRus/starter/micro"
|
"github.com/ManyakRus/starter/micro"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
//"github.com/ManyakRus/starter/common/v0/logger"
|
//"github.com/ManyakRus/starter/common/v0/logger"
|
||||||
"github.com/ManyakRus/starter/stopapp"
|
"github.com/ManyakRus/starter/stopapp"
|
||||||
)
|
)
|
||||||
@@ -54,8 +57,26 @@ func TestWaitStop(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
//ProgramDir := micro.ProgramDir_Common()
|
|
||||||
config.LoadEnv()
|
config.LoadEnv()
|
||||||
Connect()
|
Connect()
|
||||||
CloseConnection()
|
defer CloseConnection()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSendMessageCtx(t *testing.T) {
|
||||||
|
|
||||||
|
config.LoadEnv()
|
||||||
|
Connect()
|
||||||
|
defer CloseConnection()
|
||||||
|
|
||||||
|
ctxMain := contextmain.GetContext()
|
||||||
|
ctx, cancel := context.WithTimeout(ctxMain, 60*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
Subject := "TESTING"
|
||||||
|
Data := []byte("testing")
|
||||||
|
err := SendMessageCtx(ctx, Subject, Data)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("TestSendMessageCtx() error: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import "time"
|
|||||||
func StringSQLTime(time1 time.Time) string {
|
func StringSQLTime(time1 time.Time) string {
|
||||||
Otvet := ""
|
Otvet := ""
|
||||||
|
|
||||||
Otvet = "'" + time1.Format(time.RFC3339Nano) + "'"
|
format := "2006-01-02T15:04:05.999999Z07:00"
|
||||||
|
Otvet = "'" + time1.Format(format) + "'"
|
||||||
|
//Otvet = "'" + time1.Format(time.RFC3339Nano) + "'"
|
||||||
|
|
||||||
return Otvet
|
return Otvet
|
||||||
}
|
}
|
||||||
@@ -13,7 +15,7 @@ func StringSQLTime(time1 time.Time) string {
|
|||||||
func StringSQLTime_WithoutTimeZone(time1 time.Time) string {
|
func StringSQLTime_WithoutTimeZone(time1 time.Time) string {
|
||||||
Otvet := ""
|
Otvet := ""
|
||||||
|
|
||||||
format := "2006-01-02T15:04:05.999999999+00:00"
|
format := "2006-01-02T15:04:05.999999+00:00"
|
||||||
Otvet = "'" + time1.Format(format) + "'"
|
Otvet = "'" + time1.Format(format) + "'"
|
||||||
|
|
||||||
return Otvet
|
return Otvet
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ func SendMessage(phone_send_to string, text string) (int, error) {
|
|||||||
|
|
||||||
//ctxMain := contextmain.GetContext()
|
//ctxMain := contextmain.GetContext()
|
||||||
ctxMain := context.Background()
|
ctxMain := context.Background()
|
||||||
ctx, cancel := context.WithTimeout(ctxMain, 600*time.Second)
|
ctx, cancel := context.WithTimeout(ctxMain, 60*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
sender := message.NewSender(api)
|
sender := message.NewSender(api)
|
||||||
@@ -125,8 +125,11 @@ func SendMessage(phone_send_to string, text string) (int, error) {
|
|||||||
|
|
||||||
target := sender.Resolve(phone_send_to)
|
target := sender.Resolve(phone_send_to)
|
||||||
target.NoForwards()
|
target.NoForwards()
|
||||||
|
|
||||||
|
//отправка сообщения
|
||||||
UpdatesClass, err := target.Text(ctx, text)
|
UpdatesClass, err := target.Text(ctx, text)
|
||||||
|
|
||||||
|
//проверка на ошибки
|
||||||
isFlood := false
|
isFlood := false
|
||||||
if err != nil {
|
if err != nil {
|
||||||
textFind := "peer: can't resolve phone"
|
textFind := "peer: can't resolve phone"
|
||||||
@@ -256,8 +259,25 @@ func (a termAuth) Code(ctx context.Context, _ *tg.AuthSentCode) (string, error)
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Stdin, _ := io.Pipe() //нужен т.к. не работает в тест
|
||||||
|
|
||||||
|
//r, _ := io.Pipe()
|
||||||
|
//scanner := bufio.NewScanner(r)
|
||||||
|
//msg := "Enter code from telegram: "
|
||||||
|
//fmt.Fprintln(os.Stdout, msg)
|
||||||
|
//
|
||||||
|
//scanner.Scan()
|
||||||
|
//if err := scanner.Err(); err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
//}
|
||||||
|
//code := scanner.Text()
|
||||||
|
//if len(code) == 0 {
|
||||||
|
// log.Fatal("empty input")
|
||||||
|
//}
|
||||||
|
|
||||||
fmt.Print("Enter code: ")
|
fmt.Print("Enter code: ")
|
||||||
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||||
|
//code, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -284,7 +304,7 @@ func (s *memorySession) LoadSession(context.Context) ([]byte, error) {
|
|||||||
cpy, err := ioutil.ReadFile(filenameSession)
|
cpy, err := ioutil.ReadFile(filenameSession)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cpy = nil
|
cpy = nil
|
||||||
log.Info(err)
|
log.Error(err)
|
||||||
return nil, nil
|
return nil, nil
|
||||||
//return nil, session.ErrNotFound
|
//return nil, session.ErrNotFound
|
||||||
}
|
}
|
||||||
@@ -323,6 +343,10 @@ func (s *memorySession) StoreSession(ctx context.Context, data []byte) error {
|
|||||||
func CreateTelegramClient(func_OnNewMessage func(ctx context.Context, entities tg.Entities, u *tg.UpdateNewMessage) error) {
|
func CreateTelegramClient(func_OnNewMessage func(ctx context.Context, entities tg.Entities, u *tg.UpdateNewMessage) error) {
|
||||||
// https://core.telegram.org/api/obtaining_api_id
|
// https://core.telegram.org/api/obtaining_api_id
|
||||||
|
|
||||||
|
if Settings.TELEGRAM_APP_ID == 0 {
|
||||||
|
FillSettings()
|
||||||
|
}
|
||||||
|
|
||||||
programDir := micro.ProgramDir()
|
programDir := micro.ProgramDir()
|
||||||
filenameSession = programDir + "session.txt"
|
filenameSession = programDir + "session.txt"
|
||||||
|
|
||||||
@@ -397,12 +421,13 @@ func ConnectTelegram() error {
|
|||||||
|
|
||||||
bg.WithContext(ctx)
|
bg.WithContext(ctx)
|
||||||
var err error
|
var err error
|
||||||
|
//Option := bg.WithContext(ctx)
|
||||||
stopTelegramFunc, err = bg.Connect(client)
|
stopTelegramFunc, err = bg.Connect(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Can not connect to Telegram ! Error: ", err)
|
log.Fatalln("Can not connect to Telegram ! Error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
micro.Sleep(10) //не успевает
|
micro.Sleep(100) //не успевает
|
||||||
//for i := 1; i <= 5; i++ {
|
//for i := 1; i <= 5; i++ {
|
||||||
// err = client.Ping(ctx)
|
// err = client.Ping(ctx)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ func TestTimeLimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSendMessage(t *testing.T) {
|
func TestSendMessage(t *testing.T) {
|
||||||
|
var err error
|
||||||
config.LoadEnv()
|
config.LoadEnv()
|
||||||
//stopapp.StartWaitStop()
|
//stopapp.StartWaitStop()
|
||||||
|
|
||||||
@@ -44,9 +45,10 @@ func TestSendMessage(t *testing.T) {
|
|||||||
|
|
||||||
CreateTelegramClient(nil)
|
CreateTelegramClient(nil)
|
||||||
|
|
||||||
err := ConnectTelegram()
|
err = ConnectTelegram()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("telegramclient_test.TestSendMessage() error: ", err)
|
t.Error("telegramclient_test.TestSendMessage() error: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
text := "Test www.ya.ru " + time.Now().String()
|
text := "Test www.ya.ru " + time.Now().String()
|
||||||
|
|||||||
Reference in New Issue
Block a user