Warning: session_start(): open(/var/lib/php/sessions/sess_lud5o3507rqh1fkok70036v196, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
!include once #1017511 // Telegram Bots
import org.telegram.telegrambots.generics.BotSession;
static TelegramLongPollingBot bot;
static BotSession botSession;
sbool useWitAI = true;
p {
fS apiToken = telegramBotToken_mandatory();
ApiContextInitializer.init();
bot = new TelegramLongPollingBot {
public S getBotUsername() { ret "HelloComputer_bot"; }
public S getBotToken() { ret apiToken; }
public void onUpdateReceived(Update update) {
pcall {
print("Got update! " + update);
Message msg = update.getMessage(); //update.getChannelPost();
logQuotedWithDate(telegramFullUpdatesLogFile(), str(update));
if (msg == null) ret;
final Chat chat = msg.getChat();
final User from = msg.getFrom();
Voice voice = msg.getVoice();
if (voice != null) {
Int duration = voice.getDuration();
fS mimeType = voice.getMimeType();
S fileID = voice.getFileId();
print("Incoming voice message, downloading");
printStructs(+duration, +mimeType, +fileID);
replace TgFile with org.telegram.telegrambots.api.objects.File.
// Download voice message
new GetFile getFileMethod;
getFileMethod.setFileId(fileID);
TgFile tgFile = execute(getFileMethod);
downloadFileAsync(tgFile, new DownloadFileCallback {
public void onResult(TgFile file, java.io.File f) {
print("Got voice message: " + fileInfo(f));
// Assuming ogg
final File f2 = javaxDataDir("Stored Voice Messages/" + (from != null ? from.getId() : 0) + "-" + now() + ".ogg");
copyFileVerbose(f, f2);
if (useWitAI && nempty(witAITokenOpt())) thread "Wit.AI" {
File f3 = f2;
S _mimeType = mimeType;
if (eq(mimeType, "audio/ogg")) {
oggToMP3(f2, f3 = replaceFileExtension(f2, ".mp3"));
_mimeType = "audio/mpeg3";
printFileInfo(f3);
}
S text = witAI_recognizeAudio(f3, _mimeType);
if (nempty(text))
telegram_postToSendLog(
"I think you said: " + quote(text),
chat == null ? null : chat.getId());
}
}
public void onException(TgFile file, Exception e) {
print("Couldn't download voice message: " + e);
}
});
}
if (msg.hasText()) {
S text = msg.getText();
print("Incoming text: " + text);
Map map = litorderedmap(
where := chat == null ? null : chat.getId(),
type := 'heard,
date := localDateWithMilliseconds(),
originalDate := msg.getDate(),
globalID := aGlobalID(),
+text);
if (from != null)
putAll(map, litorderedmap(
userID := from.getId(),
userFirstName := from.getFirstName(),
userLastName := from.getLastName()));
logStructure(telegramLogFile(), map);
botAppendToMechList_combining("Telegram Bot Log", struct(map));
}
}
}
};
botSession = new TelegramBotsApi().registerBot(bot);
watchStructureLog_future(telegramBotToSendLog(), 100, voidfunc(Map map) {
if (eq(map.get('action), 'send)) {
long chatID = or((Long) _get(map, 'where), telegram_chatID());
S globalID = or2(getString(map, 'globalID), aGlobalID());
telegram_sendWithLogging(bot, chatID, getString(map, 'text), globalID);
putAll(map, litorderedmap(
date := localDateWithMilliseconds(),
type := 'sent,
+globalID,
+chatID));
botAppendToMechList_combining("Telegram Bot Log", struct(map));
}
});
print("Telegram Bot running.");
}
svoid cleanMeUp {
if (botSession != null) {
print("Closing bot session");
botSession.stop();
print("Closed bot session");
botSession = null;
}
}