Download Jar. Uses 33700K of libraries. Click here for Pure Java version (21691L/135K).
!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<TgFile> { 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"); } } }
Began life as a copy of #1017513
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1017517 |
Snippet name: | Telegram Bot + wit.ai speech recognition (Logs & Posts From Log) |
Eternal ID of this version: | #1017517/61 |
Text MD5: | d3b1ef6480f60583d529a9eb679dd743 |
Transpilation MD5: | 4596877b8f25cfa5655f37f58bbd364d |
Author: | stefan |
Category: | javax / networking |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-08-27 00:29:14 |
Source code size: | 6299 bytes / 170 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 685 / 1985 |
Version history: | 60 change(s) |
Referenced in: | #1017546 - Telegram Bot [Dyn Module] |