From 921a5ebd77b6d5d69c8bf1c532d6005e7d6e9b7b Mon Sep 17 00:00:00 2001 From: Sahri Riza Umami Date: Sun, 4 Mar 2018 11:52:19 +0700 Subject: [PATCH 1/5] Added README.md --- README.md | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..933f24e --- /dev/null +++ b/README.md @@ -0,0 +1,208 @@ +## telegram-bot + +A userbot interface for [Telegram](http://telegram.org). Uses [TDLib](https://github.com/tdlib/td). + +### API, Protocol documentation + +- [Telegram API](http://core.telegram.org/api) +- [MTproto protocol](http://core.telegram.org/mtproto) +- [TDLib](https://core.telegram.org/tdlib) + +### Build Instruction + +#### Linux and BSDs + +Install utils and libs: git, cmake, libssl, liblua, liblua, gperf, libconfig. + +On Ubuntu/Debian use: + +```sh +sudo apt install git build-essential cmake libssl-dev liblua5.2-dev gperf libconfig++-dev +``` + +1. Clone GitHub Repository + + ```sh + git clone --recursive https://github.com/vysheng/tdbot.git + ``` + +2. Create build folder + + ```sh + mkdir tdbot-build + ``` + +3. Enter build folder + + ```sh + cd tdbot-build + ``` + +4. Configure + + ```sh + cmake -DCMAKE_BUILD_TYPE=Release ../tdbot + ``` + +5. Start build process + + ```sh + make telegram-bot + ``` + +### Usage + +1. First of all you need to create config. Default config name is `config` which is located in `${HOME}/.telegram-bot/` or `${HOME}/.config/telegram-bot/`. +2. Then you need to login. If you want to login as bot you need to run: + + ```sh + telegram-bot -p profile-name --login --bot=bot-token + ``` + + And, if you want to login as user you need to run: + + ```sh + telegram-bot -p profile-name --login --phone=phone-number + ``` + +3. Now you can run commands. There are two ways to do it, you can use: (1) json interface (via stdin or tcp connections), or (2) lua script.\ +To understand how to serialize commands you need to read examples and tl scheme. + +### examples + +1. Json response + + ```json + {"_":"sendMessage", "chat_id":1007779878, "reply_to_message_id":0, "disable_notification":0, "from_background":0, "input_message_content":{"_":"inputMessageText", "text":"Test text here", "disable_web_preview":0, "clear_draft":0, "entities":[]}} + ``` + +1. Configuration file + + ``` + # This is an empty config file + # Feel free to put something here + + default_profile = "main"; + + main = { + # connect to totally separate telegram environment + # it is used only for tests + # false is default value + test = false; + + # folder containing data for this profile + # default value is profile name + config_directory = "main"; + + # language code. Some telegram notifications + # may use it. Default is "en" + language_code = "en"; + + # use file db. Allows files reuse after restart + # default value is true + use_file_db = true; + + # use file garbage collector. Deletes files unused for 30 days + # default value is true + use_file_gc = true; + + # use file names as specified in document description + # instead telegram-bot can use random names + # default value is true + file_readable_names = true; + + # allow accepting and creating secret chats + # default value is true + use_secret_chats = true; + + # use chat info db. Allow to send messages to chats instantly after restart + # default value is true + use_chat_info_db = true; + + # use message db + # default value is true + use_message_db = true; + + # logname. if not starts with '/' is relative to config_directory + # if empty log to stderr + # default value is empty + logname = "log.txt"; + + # log verbosity. Default value is 0 + verbosity = 2; + + # LUA script to use. If empty do not use lua. Relative to config_directory + # default value is empty + lua_script = "script.lua"; + }; + + test_dc1 = { + test = true; + verbosity = 100; + logname = "log.txt"; + }; + + # in many cases default values are OK, so config is empty + second = { + }; + + ``` + +1. Lua script + + ```lua + function vardump(value, depth, key) + local linePrefix = "" + local spaces = "" + + if key ~= nil then + linePrefix = "["..key.."] = " + end + + if depth == nil then + depth = 0 + else + depth = depth + 1 + for i=1, depth do spaces = spaces .. " " end + end + + if type(value) == 'table' then + mTable = getmetatable(value) + if mTable == nil then + print(spaces ..linePrefix.."(table) ") + else + print(spaces .."(metatable) ") + value = mTable + end + for tableKey, tableValue in pairs(value) do + vardump(tableValue, depth, tableKey) + end + elseif type(value) == 'function' or + type(value) == 'thread' or + type(value) == 'userdata' or + value == nil + then + print(spaces..tostring(value)) + else + print(spaces..linePrefix.."("..type(value)..") "..tostring(value)) + end + end + + + function dl_cb (arg, data) + vardump (data) + end + + function tdbot_update_callback (data) + if (data._ == "updateNewMessage") then + local msg = data.message + + if msg.content._ == "messageText" then + if msg.content.text == "ping" then + assert (tdbot_function ({_="sendMessage", chat_id=msg.chat_id, reply_to_message_id=msg.id, disable_notification=false, from_background=true, reply_markup=nil, input_message_content={_="inputMessageText", text="pong", disable_web_page_preview=true, clear_draft=false, entities={}, parse_mode=nil}}, dl_cb, nil)) + end + end + end + end + ``` + From 5cb851a83dc34ab3ca701eadb2c292dbff1d2bab Mon Sep 17 00:00:00 2001 From: Sahri Riza Umami Date: Tue, 29 May 2018 13:05:34 +0700 Subject: [PATCH 2/5] Updated README.md --- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 933f24e..703ed69 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A userbot interface for [Telegram](http://telegram.org). Uses [TDLib](https://gi #### Linux and BSDs -Install utils and libs: git, cmake, libssl, liblua, liblua, gperf, libconfig. +Install utils and libs: git, cmake, libssl, liblua, gperf, libconfig. On Ubuntu/Debian use: @@ -26,25 +26,33 @@ sudo apt install git build-essential cmake libssl-dev liblua5.2-dev gperf libcon git clone --recursive https://github.com/vysheng/tdbot.git ``` -2. Create build folder +2. Optional. Update `td` submodule. + + ```sh + cd tdbot + git submodule update --remote --merge + cd .. + ``` + +3. Create build folder ```sh mkdir tdbot-build ``` -3. Enter build folder +4. Enter build folder ```sh cd tdbot-build ``` -4. Configure +5. Configure ```sh cmake -DCMAKE_BUILD_TYPE=Release ../tdbot ``` -5. Start build process +6. Start build process ```sh make telegram-bot @@ -70,10 +78,44 @@ To understand how to serialize commands you need to read examples and tl scheme. ### examples -1. Json response - - ```json - {"_":"sendMessage", "chat_id":1007779878, "reply_to_message_id":0, "disable_notification":0, "from_background":0, "input_message_content":{"_":"inputMessageText", "text":"Test text here", "disable_web_preview":0, "clear_draft":0, "entities":[]}} +1. Json response (prettyfied) + + ```javascript + { + ["@type"] = "updateNewMessage", + contains_mention = false, + disable_notification = true, + message = { + ["@type"] = "message", + author_signature = "", + can_be_deleted_for_all_users = true, + can_be_deleted_only_for_self = false, + can_be_edited = true, + can_be_forwarded = true, + chat_id = "-1001234567890", + contains_unread_mention = false, + content = { + ["@type"] = "messageText", + text = { + ["@type"] = "formattedText", + entities = {}, + text = "test" + } + }, + date = 1527571645, + edit_date = 0, + id = 583008256, + is_channel_post = false, + is_outgoing = true, + media_album_id = "0", + reply_to_message_id = 0, + sender_user_id = 123456789, + ttl = 0, + ttl_expires_in = 0, + via_bot_user_id = 0, + views = 0 + } + } ``` 1. Configuration file @@ -194,15 +236,29 @@ To understand how to serialize commands you need to read examples and tl scheme. end function tdbot_update_callback (data) - if (data._ == "updateNewMessage") then + if (data["@type"] == "updateNewMessage") then local msg = data.message - if msg.content._ == "messageText" then + if msg.content["@type"] == "messageText" then if msg.content.text == "ping" then - assert (tdbot_function ({_="sendMessage", chat_id=msg.chat_id, reply_to_message_id=msg.id, disable_notification=false, from_background=true, reply_markup=nil, input_message_content={_="inputMessageText", text="pong", disable_web_page_preview=true, clear_draft=false, entities={}, parse_mode=nil}}, dl_cb, nil)) + assert (tdbot_function ({ + ["@type"] = "sendMessage", + chat_id = msg.chat_id, + reply_to_message_id = msg.id, + disable_notification = 0, + from_background = 1, + reply_markup = nil, + input_message_content = { + ["@type"] = "inputMessageText", + text = "pong", + disable_web_page_preview = 1, + clear_draft = 0, + entities = {}, + parse_mode = nil + } + } end end end end ``` - From 26852091b75dc8f42505c041d117382d1d8519c5 Mon Sep 17 00:00:00 2001 From: Sahri Riza Umami Date: Tue, 29 May 2018 13:08:12 +0700 Subject: [PATCH 3/5] Updated td --- td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/td b/td index 19ef036..cfe4d9b 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit 19ef0361f9e4bf7e45e117f2fd12307629298d5e +Subproject commit cfe4d9bdcee9305632eb228a46a95407d05b5c7a From 09c74e89e9f57bd2c1b4e9c51e6306ef2b6a9463 Mon Sep 17 00:00:00 2001 From: Sahri Riza Umami Date: Wed, 13 Jun 2018 03:19:58 +0700 Subject: [PATCH 4/5] Update README.md --- README.md | 78 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 703ed69..8edcafb 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A userbot interface for [Telegram](http://telegram.org). Uses [TDLib](https://gi #### Linux and BSDs -Install utils and libs: git, cmake, libssl, liblua, gperf, libconfig. +Install utils and libs: `git`, `cmake`, `libssl`, `liblua`, `gperf`, `libconfig`. On Ubuntu/Debian use: @@ -20,13 +20,15 @@ On Ubuntu/Debian use: sudo apt install git build-essential cmake libssl-dev liblua5.2-dev gperf libconfig++-dev ``` +Note: On Ubuntu, `gperf` is in its universe repository. So, make sure to enable this repository. + 1. Clone GitHub Repository ```sh - git clone --recursive https://github.com/vysheng/tdbot.git + git clone --recursive https://github.com/rizaumami/tdbot.git ``` -2. Optional. Update `td` submodule. +1. Optional, but highly recommended. Update `td` submodule. ```sh cd tdbot @@ -34,53 +36,81 @@ sudo apt install git build-essential cmake libssl-dev liblua5.2-dev gperf libcon cd .. ``` -3. Create build folder +1. Create build folder ```sh mkdir tdbot-build ``` -4. Enter build folder +1. Enter build folder ```sh cd tdbot-build ``` -5. Configure +1. Configure ```sh cmake -DCMAKE_BUILD_TYPE=Release ../tdbot ``` -6. Start build process +1. Start build process ```sh make telegram-bot ``` +1. The compiled `telegram-bot` binary can be found inside `tdbot-build` folder. + ### Usage 1. First of all you need to create config. Default config name is `config` which is located in `${HOME}/.telegram-bot/` or `${HOME}/.config/telegram-bot/`. -2. Then you need to login. If you want to login as bot you need to run: - - ```sh - telegram-bot -p profile-name --login --bot=bot-token - ``` - - And, if you want to login as user you need to run: - - ```sh - telegram-bot -p profile-name --login --phone=phone-number - ``` - -3. Now you can run commands. There are two ways to do it, you can use: (1) json interface (via stdin or tcp connections), or (2) lua script.\ -To understand how to serialize commands you need to read examples and tl scheme. +1. Then you need to login. You can login as: + + 1. Bot. + + ```sh + telegram-bot -p profile-name --login --bot=BOT-TOKEN + ``` + + 1. User. + + ```sh + telegram-bot -p profile-name --login --phone=PHONE-NUMBER + ``` + + Use country code in the `PHONE-NUMBER`. Example: `621234567890`, where `+62` is code for Indonesia. + +1. Now you can run commands. There are two ways to do it: + + 1. Use JSON interface (via stdin or tcp connections) + + 1. Run `telegram-bot` as a daemon. + Example, to run `telegram-bot` as a daemon and listening to port `1337`: + + ```sh + telegram-bot -dP 1337 + ``` + + 1. Enter a JSON formatted message: + + ```sh + echo -e '{"@type":"sendMessage","chat_id":-1001234567890,"input_message_content":{"@type":"inputMessageText","text":{"text":"PING!"}}}\n' | nc.traditional -w 1 127.0.0.1 1337 + ``` + + 1. Use Lua script. + + ```sh + telegram-bot -s script.lua + ``` + + To understand how to serialize commands you need to read examples and tl scheme. ### examples -1. Json response (prettyfied) +1. JSON response (prettyfied) - ```javascript + ```js { ["@type"] = "updateNewMessage", contains_mention = false, @@ -262,3 +292,5 @@ To understand how to serialize commands you need to read examples and tl scheme. end end ``` + + See [https://github.com/rizaumami/tdbot.lua](https://github.com/rizaumami/tdbot.lua), a wrapper to simplify tdbot based bot development. From a4a45495eb6698aa0f82ea1cdb24af7f76cb25bc Mon Sep 17 00:00:00 2001 From: SayedSadat Date: Fri, 29 Jun 2018 13:13:09 +0430 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8edcafb..5ee7546 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,7 @@ Note: On Ubuntu, `gperf` is in its universe repository. So, make sure to enable local msg = data.message if msg.content["@type"] == "messageText" then - if msg.content.text == "ping" then + if msg.content.text.text == "ping" then assert (tdbot_function ({ ["@type"] = "sendMessage", chat_id = msg.chat_id,