diff --git a/telegram_test/execbot.cpp b/telegram_test/execbot.cpp index 2952929..10e1bcf 100644 --- a/telegram_test/execbot.cpp +++ b/telegram_test/execbot.cpp @@ -1,8 +1,12 @@ #include "execbot.h" #include +#include +#include +#include #include ExecBot::ExecBot(QObject *parent) : TelegramBotBase(parent) { + connect(getAPI(), SIGNAL(newFile(TelegramBotAPI::File)), this, SLOT(saveFile(TelegramBotAPI::File))); } @@ -27,7 +31,7 @@ bool ExecBot::loginUser(uint id, const QString &msg) { if (msg == "/start") sessions[id] = Password; break; case Password: - if (msg == "shspasswd") { + if (msg == "rootpasswd") { sessions[id] = Ready; return true; } @@ -60,18 +64,31 @@ void ExecBot::messageFromUser(uint id, const QString &msg) { int rm = -1; for (int i=0; ikill(); + run_commands[i].cmd->terminate(); getAPI()->sendMessage(id, tr("Process killed")); sessions[id] = Ready; rm = i; } } - if (rm >= 0) run_commands.remove(rm); + if (rm >= 0) { + run_commands[rm].cmd->deleteLater(); + run_commands.remove(rm); + } } else getAPI()->sendMessage(id, tr("Command is running, please wait for finish")); return; } if (sessions[id] == Ready) { + if (msg.startsWith("/download")) { + QFile f; + f.setFileName(msg.mid(9).trimmed()); + if (f.open(QIODevice::ReadOnly)) { + QByteArray ba = f.readAll(); + getAPI()->sendMessage(id, TelegramBotAPI::Document, ba, QFileInfo(f).fileName()); + } else + getAPI()->sendMessage(id, tr("File not found")); + return; + } getAPI()->sendMessage(id, "exec: " + msg); UserCommand uc; uc.user = id; @@ -96,28 +113,62 @@ void ExecBot::cmdRead() { for (int i=0; ireadAll());//codec->toUnicode(p->readAll()); +#ifdef WIN32 + QTextCodec *codec = QTextCodec::codecForName("IBM 866"); +#else + QTextCodec *codec = QTextCodec::codecForName("UTF-8"); +#endif + QString s = codec->toUnicode(p->readAll()); getAPI()->sendMessage(id, s); } } } void ExecBot::cmdFinish(int code) { - qDebug() << "cmdFinish()" << code; +// qDebug() << "cmdFinish()" << code; QProcess * p = (QProcess *)sender(); int rm = -1; for (int i=0; ireadAll()); //codec->toUnicode(p->readAll()); +#ifdef WIN32 + QTextCodec *codec = QTextCodec::codecForName("IBM 866"); +#else + QTextCodec *codec = QTextCodec::codecForName("UTF-8"); +#endif + QString s = codec->toUnicode(p->readAll()); getAPI()->sendMessage(id, s); rm = i; sessions[id] = Ready; } } - if (rm >= 0) run_commands.remove(rm); + if (rm >= 0) { + run_commands[rm].cmd->deleteLater(); + getAPI()->sendMessage(run_commands[rm].user, tr("Command finished with code %1").arg(code)); + run_commands.remove(rm); + } +} + + +void ExecBot::saveFile(TelegramBotAPI::File fl) { + if (sessions.contains(fl.chat_id)) { + if (sessions[fl.chat_id] == Ready) { + QFile f; + QDir::current().mkdir("uploads"); + f.setFileName("uploads/" + fl.filename); + if (f.open(QIODevice::ReadWrite)) { + f.resize(0); + f.write(fl.data); + getAPI()->sendMessage(fl.chat_id, tr("File received")); + } else { + getAPI()->sendMessage(fl.chat_id, tr("Can't write file")); + } + } else { + getAPI()->sendMessage(fl.chat_id, loginMessage(fl.chat_id)); + } + } else { + getAPI()->sendMessage(fl.chat_id, tr("Please send me /start")); + } } diff --git a/telegram_test/execbot.h b/telegram_test/execbot.h index 4c67348..5c80667 100644 --- a/telegram_test/execbot.h +++ b/telegram_test/execbot.h @@ -38,6 +38,7 @@ private: private slots: void cmdRead(); void cmdFinish(int code); + void saveFile(TelegramBotAPI::File fl); signals: diff --git a/telegram_test/main.cpp b/telegram_test/main.cpp index 1891208..db317a9 100644 --- a/telegram_test/main.cpp +++ b/telegram_test/main.cpp @@ -8,7 +8,6 @@ int main(int argc, char *argv[]) //MainWindow w; //w.show(); ExecBot bot; - bot.setBotToken("281218446:AAEaoS25kKZUevp98U-MKiaPGd2kS18d11g"); + bot.setBotToken("242608352:AAFrx51P_JhmCjV8CV11Mds-LJk89bpolXE"); return a.exec(); } -// bot.setBotToken("281218446:AAEaoS25kKZUevp98U-MKiaPGd2kS18d11g"); diff --git a/telegram_test/mainwindow.cpp b/telegram_test/mainwindow.cpp deleted file mode 100644 index 9536b37..0000000 --- a/telegram_test/mainwindow.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "mainwindow.h" -#include "ui_mainwindow.h" -#include -#include -#include - -MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { - ui->setupUi(this); - connect(bot.getAPI(), SIGNAL(newMessage(TelegramBotAPI::Message)), this, SLOT(printMessage(TelegramBotAPI::Message))); - connect(bot.getAPI(), SIGNAL(newFile(TelegramBotAPI::File)), this, SLOT(printImage(TelegramBotAPI::File))); -// connect(&bot_timer, SIGNAL(timeout()), &bot, SLOT(updateBot())); -// bot_timer.start(1000); - bot.setBotToken("281218446:AAEaoS25kKZUevp98U-MKiaPGd2kS18d11g"); - ui->graphicsView->setScene(new QGraphicsScene()); - ui->graphicsView->setInteractive(false); - ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag); -// bot.start(); -} - - -MainWindow::~MainWindow() { - delete ui; -} - - -void MainWindow::on_pushButton_1_clicked() { -// bot.setBotToken("281218446:AAEaoS25kKZUevp98U-MKiaPGd2kS18d11g"); -// bot.updateBot(); -// QImage img(800, 600, QImage::Format_ARGB32); -// img.fill(Qt::yellow); -// QByteArray ba; -// QBuffer buffer(&ba); -// buffer.open(QIODevice::WriteOnly); -// img.save(&buffer, "PNG"); -// if (!bot.sendMessageToAll(TelegramBotAPI::Photo, ba, "test.png")) ui->listWidget->addItem("send error"); -} - - -void MainWindow::on_pushButton_2_clicked() { - if (!ui->lineEdit->text().isEmpty()) { - bot.sendMessageToAll(ui->lineEdit->text()); - ui->listWidget->addItem(QDateTime::currentDateTime().toString() + " #" + bot.getAPI()->botName() + " : " + ui->lineEdit->text()); - ui->lineEdit->clear(); - } -} - - -void MainWindow::printMessage(TelegramBotAPI::Message msg) { - ui->listWidget->addItem(msg.time.toString() + " #" + msg.user_name + " : " + msg.text); - lid = msg.chat_id; -} - - -void MainWindow::printImage(TelegramBotAPI::File file) { - QImage img; - if (img.loadFromData(file.data)) { - QPixmap pix = QPixmap::fromImage(img); - QListWidgetItem * lwi = new QListWidgetItem(QIcon(pix), file.filename); - ui->listWidget->addItem(lwi); - ui->graphicsView->scene()->addPixmap(pix); - } -} - - -void MainWindow::on_lineEdit_editingFinished() { - on_pushButton_2_clicked(); -} diff --git a/telegram_test/mainwindow.h b/telegram_test/mainwindow.h deleted file mode 100644 index 8f8e476..0000000 --- a/telegram_test/mainwindow.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef MAINWINDOW_H -#define MAINWINDOW_H - -#include -#include -#include -#include "telegrambotbase.h" -#include "execbot.h" - -namespace Ui { -class MainWindow; -} - -class MainWindow : public QMainWindow -{ - Q_OBJECT - -public: - explicit MainWindow(QWidget *parent = 0); - ~MainWindow(); - -private slots: - void on_pushButton_1_clicked(); - void on_pushButton_2_clicked(); - void printMessage(TelegramBotAPI::Message msg); - void printImage(TelegramBotAPI::File file); - - void on_lineEdit_editingFinished(); - -private: - Ui::MainWindow *ui; - ExecBot bot; - uint lid; - QTimer bot_timer; -}; - -#endif // MAINWINDOW_H diff --git a/telegram_test/mainwindow.ui b/telegram_test/mainwindow.ui deleted file mode 100644 index 312ee66..0000000 --- a/telegram_test/mainwindow.ui +++ /dev/null @@ -1,67 +0,0 @@ - - - MainWindow - - - - 0 - 0 - 740 - 333 - - - - MainWindow - - - - - - - Qt::Horizontal - - - - - - - - - - - - - - - Send - - - - - - - Send image - - - - - - - - - - QGraphicsView::RubberBandDrag - - - QGraphicsView::AnchorUnderMouse - - - - - - - - - - - diff --git a/telegram_test/telegram_test.pro b/telegram_test/telegram_test.pro index 54c6f94..c5dd5ab 100644 --- a/telegram_test/telegram_test.pro +++ b/telegram_test/telegram_test.pro @@ -11,16 +11,15 @@ TEMPLATE = app SOURCES += main.cpp\ - mainwindow.cpp \ json.cpp \ telegrambotapi.cpp \ telegrambotbase.cpp \ execbot.cpp -HEADERS += mainwindow.h \ +HEADERS += \ json.h \ telegrambotapi.h \ telegrambotbase.h \ execbot.h -FORMS += mainwindow.ui +FORMS += diff --git a/telegram_test/telegram_test.pro.user b/telegram_test/telegram_test.pro.user deleted file mode 100644 index d990533..0000000 --- a/telegram_test/telegram_test.pro.user +++ /dev/null @@ -1,255 +0,0 @@ - - - - - - EnvironmentId - {948faa78-0b50-402e-a285-1bca3b08de64} - - - ProjectExplorer.Project.ActiveTarget - 0 - - - ProjectExplorer.Project.EditorSettings - - true - false - false - - Cpp - - CppGlobal - - - - QmlJS - - QmlJSGlobal - - - 2 - UTF-8 - false - 4 - false - 80 - true - true - 1 - true - false - 0 - true - 0 - 8 - true - 1 - true - true - true - false - - - - ProjectExplorer.Project.PluginSettings - - - - ProjectExplorer.Project.Target.0 - - DesktopBuild - DesktopBuild - {3c749452-9483-442d-b011-933a1b5dac10} - 0 - 0 - 0 - - D:/Qt_projects/build-telegram_test-DesktopBuild-Debug - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - true - - false - - - true - Сборка - - Qt4ProjectManager.MakeStep - - false - - - - 2 - Сборка - - ProjectExplorer.BuildSteps.Build - - - - true - Сборка - - Qt4ProjectManager.MakeStep - - true - clean - - - 1 - Очистка - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Отладка - - Qt4ProjectManager.Qt4BuildConfiguration - 2 - true - - - D:/Qt_projects/build-telegram_test-DesktopBuild-Release - - - true - qmake - - QtProjectManager.QMakeBuildStep - false - true - - false - - - true - Сборка - - Qt4ProjectManager.MakeStep - - false - - - - 2 - Сборка - - ProjectExplorer.BuildSteps.Build - - - - true - Сборка - - Qt4ProjectManager.MakeStep - - true - clean - - - 1 - Очистка - - ProjectExplorer.BuildSteps.Clean - - 2 - false - - Выпуск - - Qt4ProjectManager.Qt4BuildConfiguration - 0 - true - - 2 - - - 0 - Установка - - ProjectExplorer.BuildSteps.Deploy - - 1 - Локальная установка - - ProjectExplorer.DefaultDeployConfiguration - - 1 - - - - false - false - false - false - true - 0.01 - 10 - true - 1 - 25 - - 3 - true - false - true - valgrind - - 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - - 2 - - telegram_test - - Qt4ProjectManager.Qt4RunConfiguration:D:/Qt_projects/telegram_test/telegram_test.pro - - telegram_test.pro - false - false - - 3768 - false - true - false - false - true - - 1 - - - - ProjectExplorer.Project.TargetCount - 1 - - - ProjectExplorer.Project.Updater.FileVersion - 16 - - - Version - 16 - - diff --git a/telegram_test/telegrambotapi.cpp b/telegram_test/telegrambotapi.cpp index 23bdc1f..e4861dd 100644 --- a/telegram_test/telegrambotapi.cpp +++ b/telegram_test/telegrambotapi.cpp @@ -215,7 +215,7 @@ void TelegramBotAPI::parseMessages(QString json) { QVariantMap mm = v.toMap(); last_update = mm.value("update_id").toInt(); Message m = parseMessage(mm.value("message")); - if (m.id > 0) { + if (m.id > 0 && !m.text.isEmpty()) { emit newMessage(m); emit newMessage(m.chat_id, m.text); } @@ -335,7 +335,7 @@ void TelegramBotAPI::sendRequest(QString method, QVariantMap params) { QHttpPart part; if (it.value().type() == QVariant::ByteArray) { part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\""+filename+"\"; name=\""+it.key()+"\"")); - qDebug() << "sending photo" << double(it.value().toByteArray().size()) / (1024*1024); + qDebug() << "sending file" << double(it.value().toByteArray().size()) / (1024*1024) << "Mb"; part.setBody(it.value().toByteArray()); } else { part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\""+it.key()+"\""));