Skip to content

Commit

Permalink
Merge pull request #10 from unixMiB/feature/post-as-user, fixes #5
Browse files Browse the repository at this point in the history
Merge feature/post as user, fixes #5
  • Loading branch information
kowalski7cc authored Mar 18, 2019
2 parents abc7cbd + fd17994 commit 42a6b66
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 95 deletions.
26 changes: 24 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,29 @@
</properties>

<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>mercatino.version</name>
<value>${project.version}</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
Expand Down Expand Up @@ -137,6 +158,7 @@
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>generic</id>
<phase>package</phase>
<goals>
<goal>single</goal>
Expand Down Expand Up @@ -169,7 +191,7 @@
<dependency>
<groupId>com.xspacesoft.kowalski7cc</groupId>
<artifactId>BotRevolution</artifactId>
<version>5.0b-Snapshot</version>
<version>5.0c-Snapshot</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
79 changes: 75 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,84 @@
# Mercatino unixMiB

## Che cosa è il mercatino?
## Che cosa è il mercatino

Il mercatino dell'usato di unixMiB ti permette di creare inserzioni per vendere quello che non usi più.

## Cosa ho bisogno per accedere al mercatino?
## Cosa ho bisogno per accedere al mercatino

Per utilizzare il mercatino è sufficiente un account [Telegram](https://telegram.org/) per iniziare subito a chattare.

## Come accedo al mercatino?
## Come accedo al mercatino

Per avviare una chat con il bot recati alla pagina [https://t.me/unixmib_mercatino_bot](https://t.me/unixmib_mercatino_bot) o cerca [@unixmib_mercatino_bot](https://t.me/unixmib_mercatino_bot) in Telegram
Per iniziare una chat con il bot recati alla pagina [https://t.me/unixmib_mercatino_bot](https://t.me/unixmib_mercatino_bot) o cerca [@unixmib_mercatino_bot](https://t.me/unixmib_mercatino_bot) in Telegram

## Configurazione del bot

- Bot token di Telegram da [@botfather](https://t.me/botfather)
- ID del canale da usare come bacheca del mercatino
- ID degli amministratori iniziali (opzionale)
- ID del gruppo di amministrazione (opzionale)


### Parametri mediante file JSON

`config.json`

```json
{
"token": "Token del bot",
"board": "ID del canale usato come bacheca",
"admins": [123456, 123456, 123456],
"group": "ID del gruppo di amministratori"
}
```

### Parametri mediante variabili d'ambiente

Configurazione consigliata con un container Docker

- `MERCATUNO_TOKEN` = "Token del bot"
- `MERCATINO_BOARD` = "ID del canale usato come bacheca"
- `MERCATINO_ADMINS` = 123456:123456:123456
- `MERCATINO_GROUP` = "ID del gruppo di amministratori"

Un esempio nella riga di comando sarà:

```bash
java -DMERCATUNO_TOKEN=token -DMERCATINO_BOARD=id -DMERCATINO_ADMINS=id:id:id -DMERCATINO_GROUP=id -jar mercatino.jar
```

### Parametri mediante argomento

- `-t`: Token del bot
- `-b`: ID del canale usato come bacheca
- `-a`: Stringa di amministratori separata da ':'
- `-g`: ID del gruppo di amministratori

Un esempio nella riga di comando sarà:

```bash
java -jar mercatino.jar -t token -b id -a 123456:123456:123456 -g id
```

## Docker container tags

- `190304`, `190304-alpine`, `alpine`, `latest` (190304/alpine)

- `190304-windowsservercore`, `190304-windowsservercore-ltsc2019`, `windowsservercore`, `windowsservercore-ltsc2019` (190304/windows/servercore/ltsc2019)

- `190303`, `190303-alpine` (190303/alpine)

- `190303-windowsservercore`, `190303-windowsservercore-ltsc2019` (190303/windows/servercore/ltsc2019)

- `190228`, `190228-alpine` (190228/alpine)

- `190228-windowsservercore`, `190228-windowsservercore-ltsc2019` (199228/windows/servercore/ltsc2019)

- `190223`, `190223-stretch`, `stretch` (190223/stretch)

- `190223-windowsservercore`, `190223-windowsservercore-ltsc2019` (190223/windows/servercore/ltsc2019)

### Shared tags

- `latest`
9 changes: 9 additions & 0 deletions src/main/java/io/github/unixmib/mercatino/Advertisement.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import com.kowalski7cc.botrevolution.types.media.PhotoSize;

import java.util.List;
import java.util.Optional;

public class Advertisement {

private User owner;
private Optional<Integer> publisherOverride;
private List<PhotoSize> photoSizes;
private String title;
private String description;
Expand All @@ -49,6 +51,13 @@ public Advertisement setOwner(User owner) {
return this;
}

public Optional<Integer> getPublisherOverride() { return publisherOverride;}

public Advertisement setPublisherOverride(Optional<Integer> publisherOverride) {
this.publisherOverride = publisherOverride;
return this;
}

public List<PhotoSize> getPhotoSizes() {
return photoSizes;
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/github/unixmib/mercatino/BotCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2019 Kowalski7cc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.github.unixmib.mercatino;

public interface BotCommand {

String getCommand();

String getState();
}
73 changes: 60 additions & 13 deletions src/main/java/io/github/unixmib/mercatino/BotLogic.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* Copyright (C) 2019 Kowalski7cc
*
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* <p>
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand All @@ -23,12 +23,14 @@
import com.kowalski7cc.botrevolution.types.repymarkups.replykeyboards.ReplyKeyboardBuilder;
import com.kowalski7cc.botrevolution.types.repymarkups.replykeyboards.ReplyKeyboardRemove;

import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;

import static java.util.Optional.*;

public class BotLogic {

public static StatesManager<Message> load(StatesManager<Message> statesManager, TelegramBot telegramBot) {
public static StatesManager<Message> loadFSM(StatesManager<Message> statesManager, TelegramBot telegramBot) {

statesManager.newState("start", o -> {
telegramBot.sendMessage().setChatID(o.getChat()).setText("Ciao" + o.getFrom()
Expand Down Expand Up @@ -67,6 +69,18 @@ public static StatesManager<Message> load(StatesManager<Message> statesManager,
message.getChat().getPrivateChat()
.ifPresent(privateChat -> statesManager.store
.put("advertisement", new Advertisement(privateChat.toUser())));

// Check if message contains publish id override command and user is admin, then set it if true
message.getFrom().ifPresent(user -> DataStore.getAdmins()
.stream()
.filter(user.getId()::equals)
.findFirst()
.ifPresentOrElse(integer -> new CommandParser(message).getParameters().ifPresent(s ->
{
getAdvertisementData(statesManager).setPublisherOverride(of(Integer.parseInt(s)));
System.out.println("OVERRIDE MODE: " + user.getId() + " -> " + s);
}), () -> getAdvertisementData(statesManager).setPublisherOverride(empty())));

telegramBot.sendMessage()
.setChatID(message.getChat())
.setText("Inviami il titolo della tua inserzione")
Expand Down Expand Up @@ -144,40 +158,40 @@ public static StatesManager<Message> load(StatesManager<Message> statesManager,


statesManager.newState("read_publish", message -> message.getText()
.map(s -> Optional.of(s)
.map(s -> of(s)
.filter(s1 -> s1.equals("Pubblica"))
.map(s1 -> {
var adv = getAdvertisementData(statesManager);
String uuid = UUID.randomUUID().toString();
DataStore.getAdvertisementMap().put(uuid, adv);
DataStore.getAdmins().forEach(aLong -> {
DataStore.getAdvertisements().put(uuid, adv);
DataStore.getAdmins().forEach(id -> {
try {
telegramBot.sendPhoto()
.setChatID(aLong)
.setChatID(id.longValue())
.setCaption(adv.getTitle() + "\n" + adv.getDescription())
.setPhoto(adv.getPhotoSizes().get(0).getFileID())
.setReplyMarkup(new InlineKeyboardBuilder().addRow()
.buildButton("Approva")
.setCallbackData("publish:" + uuid)
.build()
.buildButton("Cancella")
.setCallbackData("delete:"+uuid)
.setCallbackData("delete:" + uuid)
.build()
.build().build())
.send();
} catch (Exception e) {
System.out.println("Inpossibile contattare l'admin " + aLong);
System.out.println("MODERATION ERROR: Impossibile contattare l'admin " + id);
}
});

telegramBot.sendMessage()
.setChatID(message.getChat())
.setText("Ottimo. Il tuo annuncio sarà presto pubblicato")
.setReplyMarkup(new ReplyKeyboardRemove())
.send();
.send();
return "show_hint";
})
.or(() -> Optional.of(s).filter(s1 -> s1.equals("Annulla")).map(s1 -> {
.or(() -> of(s).filter(s1 -> s1.equals("Annulla")).map(s1 -> {
telegramBot.sendMessage()
.setChatID(message.getChat())
.setText("Pubblicazione annullata")
Expand Down Expand Up @@ -213,6 +227,39 @@ public static StatesManager<Message> load(StatesManager<Message> statesManager,
return "show_hint";
});

statesManager.newState("selfpromote", message -> {
Consumer<String> msg = text -> telegramBot.sendMessage()
.setChatID(message.getChat())
.setText(text)
.send();
Runnable error = () -> msg.accept("Errore durante l'elaborazione");

DataStore.getAdminsGroup().ifPresentOrElse(s -> message.getFrom()
.ifPresentOrElse(user -> telegramBot.getChatMember()
.setChatID(s)
.setUserID(user)
.send()
.ifPresentOrElse(chatMember -> {
if (chatMember.getStatus().equals("left")) {
msg.accept("Non hai i privilegi per eseguire questa operazione. Questo incidente verrà segnalato.");
System.out.println("SELFPROMOTE FAILED: " + message.getFrom());
} else {
DataStore.addAdmin(user.getId());
msg.accept("Privilegi elevati con successo");
System.out.println("SELFPROMOTE SUCCEDED: " + message.getFrom());
}
}, () -> error.run()), () -> error.run()), () -> error.run());
return "show_hint";
});

statesManager.newState("unknownCommand", message -> {
telegramBot.sendMessage()
.setChatID(message.getChat())
.setText("Comando non valido")
.send();
return "show_hint";
});

statesManager.setInitialState("start");
statesManager.reset();

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/io/github/unixmib/mercatino/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2019 Kowalski7cc
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.github.unixmib.mercatino;

public enum Command implements BotCommand {

START("start", "start"),
VENDI("vendi", "new_advertisement"),
HELP("help", "show_hint"),
BACHECA("bacheca", "bacheca"),
ANNULLA("annulla", "abort"),
SELFPROMOTE("selfpromote", "selfpromote");

private String command;
private String state;

Command(String command, String state) {
this.command = command;
this.state = state;
}

@Override
public String getCommand() {
return command;
}

@Override
public String getState() {
return state;
}
}
Loading

0 comments on commit 42a6b66

Please sign in to comment.