GithubHelp home page GithubHelp logo

nim-telegram-bot's Introduction

nim-telegram-bot

  • Easy *.ini & *.md Customization. Single Binary, ~150 Kilobytes size. Tiny CPU & Net use. Plugins.

Rlyeh HackLab

Install

  • nimble install nim_telegram_bot

Compile

To compile from sources, get the Code:

git clone https://github.com/juancarlospaco/nim-telegram-bot.git
cd nim-telegram-bot/

Compile:

nim e build_nim_telegram_bot.nims

Optional, Compilation and Run for Development only (Hacks, testing, dev, etc)

nim c -r -d:ssl nim_telegram_bot.nim

Config

  • Rename the file config.ini.TEMPLATE to config.ini.
  • Edit the file config.ini to set api_key, polling_interval, etc.
  • Edit the file coc_text.md to customize Code Of Conduct text (AKA Rules).
  • Edit the file motd_text.md to customize Message Of The Day text.
  • Edit the file help_text.md to customize Help text.
  • Edit the file donate_text.md to customize Donations text.

You can hack any of the *.ini and *.md to customize.

When ever you have the error of missing *.md files, you can just use an empty file.

Check the Docs on Nimble!.

Run nim doc nim_telegram_bot.nim for more Docs!.

Plugins

On run the bot creates the following folders:

./plugins/
./plugins/bash/
./plugins/python/
./plugins/geo/
./plugins/static/

Bash scripts plugins:

./plugins/bash/ are for *.sh Bash scripts plugins, the filename must be all lowercase and not contain whitespaces and end with *.sh, the filename will be the command to trigger the plugin, eg foo.sh will be /foo on Telegram chat, output of the script will be sent as string to chat by the bot, anything you want the bot to say just print it to standard output. Comments are allowed on the SH file. File extension is case sensitive, so its *.sh, not *.SH.

Example Bash plugin:

# example.sh
echo "This is an example Bash plugin."

Python plugins:

./plugins/python/ are for *.py Python 3 plugins, the filename must be all lowercase and not contain whitespaces and end with *.py, the filename will be the command to trigger the plugin, eg lol.py will be /lol on Telegram chat, the return of the main() function of the script will be sent as string to chat by the bot, anything you want the bot to say just return it on main() as str type, the main() function name is a convention, is hardcoded and can not be changed. No other functions are needed on the INI. Comments are allowed on the Python file. File extension is case sensitive, so its *.py, not *.PY.

Example ./plugins/python/foo.py will be /foo on Telegram chat and will call foo.main().

Example Python plugin:

# example.py
def main():
    return "Example Python Plugin."

Geo Location Sharing plugins:

./plugins/geo/ are for *.ini Geo Location Sharing plugins, the filename must be all lowercase and not contain whitespaces and end with *.ini, the filename will be the command to trigger the plugin, eg bar.ini will be /bar on Telegram chat, Geo Location of the INI will be sent as Map Thumbnail and Open Street Map Link to chat by the bot, anything you want the bot to Geo Locate just add latitude and longitude to the INI. No other keys are needed on the INI. Comments are allowed on the INI. File extension is case sensitive, so its *.ini, not *.INI.

Example Geo Location Sharing plugin:

# example.ini
latitude = 55.42
longitude = 42.66

Static Files plugins:

./plugins/static/ are for *.* Static Files "plugins", the filename must be all lowercase and not contain whitespaces, the filename will be the command to trigger the plugin, eg baz.jpg will be /baz on Telegram chat, the file will be sent as attached Document file to chat by the bot, anything you want the bot to share just copy it to that folder. File extension is case sensitive, so it must be lower case.

Example Static Files plugin: Any file is Ok.

Nim-based Plugins:

Maybe in the future we implement it, but right now it has few builtin functionalities that if you want to create a new functionality using Nim just send the Pull Request, to be integrated into the Core directly instead of as a Plugin, all functionalities can be Enabled / Disabled from the config.ini anyways.

CrossCompilation On Demand

The bot will reply any valid *.nim plain text source code file with CrossCompiled stripped native binary executables for Linux and Windows, including the SHA1 CheckSums on the chat (it compiles, does not run), theres limits on total file size and total line count, it can be disabled and configured from the config.ini.

No extra hardening security features are in place for this feature, since Telegram already has Kick and Ban, check firejails or docker for this, disable if you expect malware code.

Run

./nim_telegram_bot

The source code needs the following files on the same current folder to compile:

  • coc_text.md
  • motd_text.md
  • help_text.md
  • donate_text.md

The binary executable needs the following files on the same current folder to run:

  • config.ini

Example to compile:

/home/user/bot/nim_telegram_bot.nim
/home/user/bot/coc_text.md
/home/user/bot/motd_text.md
/home/user/bot/help_text.md
/home/user/bot/donate_text.md

Example to run:

/home/user/bot/nim_telegram_bot
/home/user/bot/config.ini

Optional, you can use any Linux command like chrt, trickle, firejails, docker, rkt with the Bot too.

Requisites

For Compilation only!, if it compiles it does not need Nim nor Telebot.

Optional For Photos on-demand:

  • 1 Working USB WebCam Camera on /dev/video0.
  • FFMPEG (Linux package, with WebP support).

Optional For CrossCompilation on-demand:

  • strip (Linux package).
  • upx (Linux package).
  • sha1sum (Linux package).

Optional For Python Plugins:

  • python (3.7+, older Python versions maybe works but its not supported).

Optional For Web Screenshots on-demand:

Optional For Python Compilation:

  • nuitka (Linux package).

Single File

Optional, this is for advanced users only.

If you want to compile to 1 file, without any extra *.md files.

On the source code find and remove the lines:

helps_texts = readFile("help_text.md")
coc_text =    readFile("coc_text.md")
motd_text =   readFile("motd_text.md")
donate_text = readFile("donate_text.md")

On the source code find and uncomment the lines:

helps_texts = staticRead("help_text.md")
coc_text =    staticRead("coc_text.md")
motd_text =   staticRead("motd_text.md")
donate_text = staticRead("donate_text.md")

Recompile, it will Embed all the *.md files on the binary executable.

You will need to Recompile to change any content of the *.md files.

You can later delete all the *.md files.

Performance Profiling

Optional, this is for advanced developers only.

Find and uncomment the line import nimprof on nim_telegram_bot.nim.

nim c --profiler:on --stacktrace:on -d:ssl -d:release --app:console --opt:size nim_telegram_bot.nim
./nim_telegram_bot

Then open the file profile_results.txt.

Check code

Optional, this is for advanced developers only.

How to Lint the code.

nimble check
nim check src/nim_telegram_bot.nim

CrossCompile

Optional, this is for advanced developers only.

Linux -> Windows, this allows to generate a *.EXE for Windows on Linux.

On Linux install all this packages:

mingw-w64-binutils mingw-w64-crt mingw-w64-gcc mingw-w64-headers mingw-w64-winpthreads mingw-w64-gcc-base mingw-w64-*

Usually only installing mingw-w64-gcc gets all the rest as dependency.

Names are from ArchLinux AUR, should be similar on other Distros

Contributions

nim-telegram-bot's People

Contributors

juancarlospaco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nim-telegram-bot's Issues

Have a proc to filter badwords.

Have a proc to filter badwords.

proc takes string, proc returns string,
but removing the "badwords", case-insensitive, badwords come from a list passed as argument.

URL 2 Screenshot

You send the bot a string message starting with http:// or https://
it sends you a high quality big screenshot of the link.

DRY

Code feels repetitive, refactor somehow to DRY,
templates or something, make code less repetitive,
also plugins need to be >10 if user wants, like a flexible sized array or json or something.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.