!7 !include once #1017511 // Telegram Bots import org.telegram.telegrambots.generics.BotSession; import org.telegram.telegrambots.api.objects.inlinequery.InlineQuery; import org.telegram.telegrambots.api.methods.*; import org.telegram.telegrambots.api.objects.inlinequery.result.*; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.*; static TelegramLongPollingBot bot; static BotSession botSession; sbool useWitAI = true; // only on Linux (but that can be changed) & when you have a token 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); InlineQuery iq = update.getInlineQuery(); if (iq != null) { answerInlineQuery(new AnswerInlineQuery() .setInlineQueryId(iq.getId()) .setResults(new InlineQueryResultArticle() .setId(aGlobalID()) .setTitle("Title.") .setDescription("Description.") .setInputMessageContent( new InputTextMessageContent() .setMessageText("Hello there. You said: " + iq.getQuery())))); } Message msg = update.getMessage(); //update.getChannelPost(); logQuotedWithDate(telegramFullUpdatesLogFile(), str(update)); if (msg == null) ret; final Chat chat = msg.getChat(); final User from = msg.getFrom(); final Map map = litorderedmap( where := chat == null ? null : chat.getId(), type := 'heard, date := localDateWithMilliseconds(), globalID := aGlobalID()); 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")) { pcall { oggToMP3(f2, f3 = replaceFileExtension(f2, ".mp3")); } if (fileEmpty(f3)) fail("Can't convert to mp3"); _mimeType = "audio/mpeg3"; printFileInfo(f3); } fS text = witAI_recognizeAudio(f3, _mimeType); if (nempty(text)) { putAll(map, litorderedmap( +text, fromVoice := true)); logStructure(telegramLogFile(), map); botAppendToMechList_combining("Telegram Bot Log", struct(map)); // Allow think bot to answer, then post what was said doAfter(5000, r { 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); putAll(map, litorderedmap( originalDate := msg.getDate(), +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()); S voice = getString(map, 'voice); S text = getString(map, 'text); if (voice != null) telegram_sendVoiceMessageWithLogging(bot, chatID, text, voice, globalID); else telegram_sendWithLogging(bot, chatID, 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) { final BotSession _botSession = botSession; botSession = null; if (vmExiting()) { print("Closing bot session during VM close"); // As stop() takes unusually long, don't bother waiting the whole time joinThreadForSomeTime(startThread(r { _botSession.stop(); }), 2000); } else { print("Closing bot session with profiling"); temp relicense(); temp profileThisThreadToConsole(); _botSession.stop(); print("Closed bot session"); } } }