-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bots): add viber bot integration (#37)
- Loading branch information
Showing
14 changed files
with
341 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/codec/ViberUpdateEvtCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.abavilla.fpi.bot.codec; | ||
|
||
import com.abavilla.fpi.fw.util.MapperUtil; | ||
import com.abavilla.fpi.viber.ext.dto.ViberUpdate; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.eventbus.MessageCodec; | ||
import lombok.SneakyThrows; | ||
|
||
public class ViberUpdateEvtCodec implements MessageCodec<ViberUpdate, ViberUpdate> { | ||
|
||
@SneakyThrows | ||
@Override | ||
public void encodeToWire(Buffer buffer, ViberUpdate update) { | ||
ObjectWriter writer = MapperUtil.mapper().writerFor(ViberUpdate.class); | ||
byte[] bytes = writer.writeValueAsBytes(update); | ||
buffer.appendInt(bytes.length); | ||
buffer.appendBytes(bytes); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public ViberUpdate decodeFromWire(int pos, Buffer buffer) { | ||
// My custom message starting from this *position* of buffer | ||
|
||
// Length of JSON | ||
int length = buffer.getInt(pos); | ||
|
||
// Get JSON string by it`s length | ||
// Jump 4 because getInt() == 4 bytes | ||
pos += 4; | ||
byte[] bytes = buffer.getBytes(pos, pos + length); | ||
|
||
ObjectReader reader = MapperUtil.mapper().readerFor(ViberUpdate.class); | ||
return reader.readValue(bytes); | ||
} | ||
|
||
@Override | ||
public ViberUpdate transform(ViberUpdate update) { | ||
return update; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return this.getClass().getName(); | ||
} | ||
|
||
@Override | ||
public byte systemCodecID() { | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/entity/viber/ViberEvt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/************************************************************************* | ||
* FPI Application - Abavilla * | ||
* Copyright (C) 2022 Vince Jerald Villamora * | ||
* * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU 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 General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>.* | ||
*************************************************************************/ | ||
|
||
package com.abavilla.fpi.bot.entity.viber; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import com.abavilla.fpi.fw.entity.mongo.AbsMongoItem; | ||
import com.abavilla.fpi.viber.ext.dto.Message; | ||
import com.abavilla.fpi.viber.ext.dto.Sender; | ||
import io.quarkus.mongodb.panache.common.MongoEntity; | ||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@ToString(callSuper = true) | ||
@NoArgsConstructor | ||
@RegisterForReflection | ||
@MongoEntity(collection = "viber_event") | ||
public class ViberEvt extends AbsMongoItem { | ||
private String event; | ||
private LocalDateTime timestamp; | ||
private String chatHostname; | ||
private Long messageToken; | ||
private Sender sender; | ||
private Message message; | ||
private Boolean silent; | ||
private String userId; | ||
} |
60 changes: 60 additions & 0 deletions
60
fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/mapper/viber/ViberEvtMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/************************************************************************* | ||
* FPI Application - Abavilla * | ||
* Copyright (C) 2022 Vince Jerald Villamora * | ||
* * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU 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 General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>.* | ||
*************************************************************************/ | ||
|
||
package com.abavilla.fpi.bot.mapper.viber; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
import com.abavilla.fpi.bot.entity.viber.ViberEvt; | ||
import com.abavilla.fpi.fw.mapper.IDtoToEntityMapper; | ||
import com.abavilla.fpi.fw.util.DateUtil; | ||
import com.abavilla.fpi.msgr.ext.dto.MsgrMsgReqDto; | ||
import com.abavilla.fpi.viber.ext.dto.ViberUpdate; | ||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingConstants; | ||
|
||
@Mapper(componentModel = MappingConstants.ComponentModel.CDI, | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR) | ||
public interface ViberEvtMapper extends IDtoToEntityMapper<ViberUpdate, ViberEvt> { | ||
|
||
@Mapping(target = "content", source = "message.text") | ||
@Mapping(target = "recipient", source = "sender.id") | ||
@Mapping(target = "replyTo", source = "message.trackingData") | ||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "dateCreated", ignore = true) | ||
@Mapping(target = "dateUpdated", ignore = true) | ||
MsgrMsgReqDto createMsgReqFromUpdate(ViberUpdate update); | ||
|
||
default LocalDateTime timestampToLdt(Long timestamp) { | ||
if (timestamp == null) { | ||
return null; | ||
} | ||
return DateUtil.fromEpoch(timestamp); | ||
} | ||
|
||
default Long ldtToTimestamp(LocalDateTime timestamp) { | ||
if (timestamp == null) { | ||
return null; | ||
} | ||
return timestamp.toEpochSecond(ZoneOffset.UTC); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/processor/ViberMsgEvtPcsr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/****************************************************************************** | ||
* FPI Application - Abavilla * | ||
* Copyright (C) 2022 Vince Jerald Villamora * | ||
* * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU 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 General Public License for more details. * | ||
* * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. * | ||
******************************************************************************/ | ||
|
||
package com.abavilla.fpi.bot.processor; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import com.abavilla.fpi.bot.codec.ViberUpdateEvtCodec; | ||
import com.abavilla.fpi.bot.entity.viber.ViberEvt; | ||
import com.abavilla.fpi.bot.mapper.viber.ViberEvtMapper; | ||
import com.abavilla.fpi.bot.repo.ViberEvtRepo; | ||
import com.abavilla.fpi.load.ext.dto.QueryDto; | ||
import com.abavilla.fpi.msgr.ext.dto.MsgrMsgReqDto; | ||
import com.abavilla.fpi.msgr.ext.rest.ViberReqApi; | ||
import com.abavilla.fpi.telco.ext.enums.BotSource; | ||
import com.abavilla.fpi.viber.ext.dto.ViberUpdate; | ||
import io.quarkus.logging.Log; | ||
import io.quarkus.vertx.ConsumeEvent; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@ApplicationScoped | ||
public class ViberMsgEvtPcsr extends EvtPcsr<ViberUpdate, ViberReqApi, ViberEvtRepo, ViberEvt> { | ||
|
||
@Inject | ||
ViberEvtMapper mapper; | ||
|
||
@ConsumeEvent(value = "viber-msg-evt", codec = ViberUpdateEvtCodec.class) | ||
public Uni<Void> process(ViberUpdate evt) { | ||
Log.info("Received viber update event: " + evt); | ||
return processEvent(evt); | ||
} | ||
|
||
@Override | ||
protected Uni<Void> sendMsgrMsg(MsgrMsgReqDto msgReq, String fpiUser) { | ||
return msgrApi.sendMsg(msgReq, fpiUser).replaceWithVoid(); | ||
} | ||
|
||
@Override | ||
protected Uni<Void> toggleTyping(String recipient, boolean isTyping) { | ||
// viber doesn't have an API for typing | ||
return Uni.createFrom().voidItem(); | ||
} | ||
|
||
@Override | ||
protected ViberEvt mapToEntity(ViberUpdate evt) { | ||
return mapper.mapToEntity(evt); | ||
} | ||
|
||
@Override | ||
protected String getContentFromEvt(ViberUpdate evt) { | ||
return evt.getMessage().getText(); | ||
} | ||
|
||
@Override | ||
protected String getEventIdForLogging(ViberUpdate evt) { | ||
return String.valueOf(evt.getMessageToken()); | ||
} | ||
|
||
@Override | ||
protected QueryDto createLoadQueryFromEvt(ViberUpdate evt) { | ||
var query = new QueryDto(); | ||
query.setQuery(evt.getMessage().getText()); | ||
query.setBotSource(getBotSource().toString()); | ||
return query; | ||
} | ||
|
||
@Override | ||
protected MsgrMsgReqDto createMsgReqFromEvt(ViberUpdate evt) { | ||
return mapper.createMsgReqFromUpdate(evt); | ||
} | ||
|
||
@Override | ||
protected String getSenderFromEvt(ViberUpdate evt) { | ||
return evt.getSender() == null ? evt.getUserId() : evt.getSender().getId(); | ||
} | ||
|
||
@Override | ||
public BotSource getBotSource() { | ||
return BotSource.VIBER; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/repo/ViberEvtRepo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.abavilla.fpi.bot.repo; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
import com.abavilla.fpi.bot.entity.viber.ViberEvt; | ||
import com.abavilla.fpi.fw.repo.AbsMongoRepo; | ||
|
||
@ApplicationScoped | ||
public class ViberEvtRepo extends AbsMongoRepo<ViberEvt> { | ||
} |
Oops, something went wrong.