1
0
mirror of https://github.com/ManyakRus/starter.git synced 2024-11-21 18:16:31 +02:00

сделал telegram_client.go

This commit is contained in:
Nikitin Aleksandr 2023-06-01 18:02:36 +03:00
parent 0f26ce5ed8
commit db2826b6ff
9 changed files with 100 additions and 23 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@
/log.txt
/common/cover.out
/whatsapp.db
/session.txt

View File

@ -232,6 +232,7 @@ func Send_BPMN_File(BPMN_filename string) {
}
FileName = dir + FileName
log.Info("Load .bpmn file from: ", FileName)
_, err = Client.NewDeployResourceCommand().AddResourceFile(FileName).Send(ctx)
if err != nil {

View File

@ -204,6 +204,7 @@ func ProgramDir_Common() string {
substr := "/tmp/"
pos1 := strings.Index(sdir, substr)
if pos1 >= 0 {
//linux
filename = CurrentFilename()
dir = filepath.Dir(filename)
@ -216,22 +217,23 @@ func ProgramDir_Common() string {
//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\\"
pos1 = strings.Index(sdir, substr)
if pos1 >= 0 {
filename = CurrentFilename()
dir = filepath.Dir(filename)
substr := SeparatorFile() + "vendor" + SeparatorFile()
pos_vendor := strings.Index(strings.ToLower(dir), substr)
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)
substr := SeparatorFile() + "vendor" + SeparatorFile()
pos_vendor := strings.Index(strings.ToLower(dir), substr)
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)
}
}
}

View File

@ -1,8 +1,10 @@
package nats_connect
import (
"context"
"github.com/nats-io/nats.go"
"github.com/ManyakRus/starter/logger"
"github.com/ManyakRus/starter/micro"
"github.com/ManyakRus/starter/ping"
"os"
//"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
}

View File

@ -1,10 +1,13 @@
package nats_connect
import (
"context"
"github.com/ManyakRus/starter/config"
"github.com/ManyakRus/starter/contextmain"
"github.com/ManyakRus/starter/micro"
"testing"
"time"
//"github.com/ManyakRus/starter/common/v0/logger"
"github.com/ManyakRus/starter/stopapp"
)
@ -54,8 +57,26 @@ func TestWaitStop(t *testing.T) {
}
func TestConnect(t *testing.T) {
//ProgramDir := micro.ProgramDir_Common()
config.LoadEnv()
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)
}
}

View File

@ -5,7 +5,9 @@ import "time"
func StringSQLTime(time1 time.Time) string {
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
}
@ -13,7 +15,7 @@ func StringSQLTime(time1 time.Time) string {
func StringSQLTime_WithoutTimeZone(time1 time.Time) string {
Otvet := ""
format := "2006-01-02T15:04:05.999999999+00:00"
format := "2006-01-02T15:04:05.999999+00:00"
Otvet = "'" + time1.Format(format) + "'"
return Otvet

View File

View File

@ -115,7 +115,7 @@ func SendMessage(phone_send_to string, text string) (int, error) {
//ctxMain := contextmain.GetContext()
ctxMain := context.Background()
ctx, cancel := context.WithTimeout(ctxMain, 600*time.Second)
ctx, cancel := context.WithTimeout(ctxMain, 60*time.Second)
defer cancel()
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.NoForwards()
//отправка сообщения
UpdatesClass, err := target.Text(ctx, text)
//проверка на ошибки
isFlood := false
if err != nil {
textFind := "peer: can't resolve phone"
@ -256,8 +259,25 @@ func (a termAuth) Code(ctx context.Context, _ *tg.AuthSentCode) (string, error)
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: ")
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
//code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
@ -284,7 +304,7 @@ func (s *memorySession) LoadSession(context.Context) ([]byte, error) {
cpy, err := ioutil.ReadFile(filenameSession)
if err != nil {
cpy = nil
log.Info(err)
log.Error(err)
return nil, nil
//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) {
// https://core.telegram.org/api/obtaining_api_id
if Settings.TELEGRAM_APP_ID == 0 {
FillSettings()
}
programDir := micro.ProgramDir()
filenameSession = programDir + "session.txt"
@ -397,12 +421,13 @@ func ConnectTelegram() error {
bg.WithContext(ctx)
var err error
//Option := bg.WithContext(ctx)
stopTelegramFunc, err = bg.Connect(client)
if err != nil {
log.Fatalln("Can not connect to Telegram ! Error: ", err)
}
micro.Sleep(10) //не успевает
micro.Sleep(100) //не успевает
//for i := 1; i <= 5; i++ {
// err = client.Ping(ctx)
// if err != nil {

View File

@ -35,6 +35,7 @@ func TestTimeLimit(t *testing.T) {
}
func TestSendMessage(t *testing.T) {
var err error
config.LoadEnv()
//stopapp.StartWaitStop()
@ -44,9 +45,10 @@ func TestSendMessage(t *testing.T) {
CreateTelegramClient(nil)
err := ConnectTelegram()
err = ConnectTelegram()
if err != nil {
t.Error("telegramclient_test.TestSendMessage() error: ", err)
return
}
text := "Test www.ya.ru " + time.Now().String()