BastliBridge

A bot framework bridgin multiple IM protocols, and mail
git clone git://xatko.vsos.ethz.ch/BastliBridge.git
Log | Files | Refs | Submodules

commit 239e9bb2161ac0b8cac7e05fcc4b66db404ba03a
parent 0459c3abfcd0e894eb74e8882c5a56d9fba748b5
Author: Dominik Schmidt <das1993@hotmail.com>
Date:   Sun, 24 Sep 2017 00:16:32 +0200

Parse more elements from Telegram-messages

Diffstat:
src/telegram.d | 103++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 102 insertions(+), 1 deletion(-)

diff --git a/src/telegram.d b/src/telegram.d @@ -436,6 +436,107 @@ struct Bot{ telegram=Telegram("253031348:AAGn4dHCPwFDSN4Bmt4ZWc3bIAmARmzsf9k"); } + static immutable string unknown_username="Unknown"; + static string TelegramUser(JSONValue User){ + string username=unknown_username; + foreach(key; ["username", "first_name", "last_name"]){ + if(key in User){ + username=User[key].str; + break; + } + } + return username; + } + + string formatProxyURL(string file_id){ + return "*FILE*"; //Stub. Implement this + } + + string locationToOSMUrl(JSONValue loc){ + return locationToOSMUrl(loc["latitude"].floating, loc["longitude"].floating); + } + string locationToOSMUrl(float lat, float lng){ + return format!"https://www.openstreetmap.org/#map=%f/%f"(lat, lng); + } + + string TelegramUserToIRC(JSONValue user){ + dchar mode=' '; + if(user["is_bot"].type==JSON_TYPE.TRUE){ + mode='*'; + } + return "<"~mode.to!string~TelegramUser(user)~">"; + } + + void TelegramToIRC(JSONValue Message, ref Appender!string app){ + string username="< "~unknown_username~">"; + if("from" in Message){ + app~=TelegramUserToIRC(Message["from"]); + app~=" "; + } + if("forward_from" in Message){ + app~=TelegramUserToIRC(Message["forward_from"]); + app~=" "; + } + foreach(t; ["text", "caption"]){ + if(t in Message){ + app~=Message[t].str; + app~=" "; + } + } + + if("photo" in Message){ + string fid; + long size=long.min; + foreach(p; Message["photo"].array){ + if(p["file_size"].integer>size){ + size=p["file_size"].integer; + fid=p["file_id"].str; + } + } + app~=formatProxyURL(fid); + app~=" "; + } + + foreach(t; ["audio", "document", "sticker", "video", "voice"]){ + if(t in Message){ + app~=formatProxyURL(Message[t]["file_id"].str); + app~=" "; + } + } + if("location" in Message){ + app~=locationToOSMUrl(Message["location"]); + app~=" "; + } + if("contact" in Message){ + auto c=Message["contact"]; + app~=c["first_name"].str; + app~=" "; + if("last_name" in c){ + app~=c["last_name"].str; + app~=" "; + } + app~="("; + app~=c["phone_number"].str; + app~=") "; + } + if("venue" in Message){ + app~=Message["venue"]["title"].str; + app~=locationToOSMUrl(Message["venue"]["location"]); + app~=" "; + } + if("reply_to_message" in Message){ + TelegramToIRC(Message["Reply_to_message"], app); + } + } + + string TelegramToIRC(JSONValue Message){ + Appender!string app; + + TelegramToIRC(Message, app); + + return app.data[0..$-1]; + } + void initialize(){ ircAddress=getAddress("irc.freenode.net",6697)[0]; auto af=ircAddress.addressFamily; @@ -555,7 +656,7 @@ struct Bot{ warning("Input on not-connected telegram-channel "~j["chat"].toPrettyString); } foreach(chan; irc_chans){ - ircClient.send(chan, "< "~j["from"]["username"].str~"> "~msg); + ircClient.send(chan, TelegramToIRC(j)); } } };