├── requirements.txt ├── Dockerfile ├── LICENSE ├── .gitignore ├── bot.py └── README.md /requirements.txt: -------------------------------------------------------------------------------- 1 | python-telegram-bot 2 | slackclient 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.5 2 | 3 | ADD requirements.txt / 4 | RUN pip install -r requirements.txt 5 | 6 | ADD bot.py / 7 | RUN chmod a+x /bot.py 8 | CMD ["python", "bot.py"] 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import time 4 | 5 | from slackclient import SlackClient 6 | from telegram.bot import Bot 7 | 8 | SLACK_TOKEN = os.environ['SLACK_TOKEN'] 9 | sc = SlackClient(SLACK_TOKEN) 10 | 11 | TELEGRAM_TOKEN = os.environ['TELEGRAM_TOKEN'] 12 | TELEGRAM_TARGET = os.environ['TELEGRAM_TARGET'] 13 | telegram_bot = Bot(TELEGRAM_TOKEN) 14 | 15 | if sc.rtm_connect(): 16 | while True: 17 | messages = sc.rtm_read() 18 | for message in messages: 19 | try: 20 | if message['type'] == 'message': 21 | if message['channel'][0] == 'G': 22 | channel = sc.api_call( 23 | "groups.info", 24 | channel=message['channel'])['group'] 25 | elif message['channel'][0] == 'C': 26 | channel = sc.api_call( 27 | "channels.info", 28 | channel=message['channel'])['channel'] 29 | else: 30 | channel = {'name': 'bot'} 31 | 32 | user = sc.api_call( 33 | "users.info", 34 | user=message['user'])['user'] 35 | 36 | msg_string = '@{} posted to #{}: {}'.format(user['name'], channel['name'], message['text']) 37 | telegram_bot.sendMessage(TELEGRAM_TARGET, msg_string) 38 | except: 39 | print('Could not send message.') 40 | print("Unexpected error:", sys.exc_info()[0]) 41 | time.sleep(1) 42 | else: 43 | print("Connection Failed, invalid token?") 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # slack-to-telegram-bot 2 | Bot for forwarding slack messages to telegram. 3 | 4 | ## Usage 5 | Tested on Python 3.5. 6 | 7 | For configuration, set the following environment variables: 8 | ``` 9 | $ export SLACK_TOKEN='' # Slack bot token 10 | $ export TELEGRAM_TOKEN='' # Telegram bot token 11 | $ export TELEGRAM_TARGET='' # Target chat 12 | ``` 13 | For the target chat, see http://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id-ruby-gem-telegram-bot. 14 | 15 | Run with: 16 | ``` 17 | python bot.py # <- You could not have guessed that! 18 | ``` 19 | You can also build and use the docker image: 20 | ``` 21 | docker build -t . 22 | docker push 23 | docker run -d --name slack-to-telegram-bot --restart=always -e TELEGRAM_TOKEN='$TELEGRAM_TOKEN' -e TELEGRAM_TARGET='$TELEGRAM_TARGET' -e SLACK_TOKEN='$SLACK_TOKEN' 24 | ``` 25 | 26 | ## Other implementations 27 | 28 | There is also [PHP version of this project](https://github.com/421p/slack-to-telegram-bot) (same api, few additional features) maintained by [421p](https://github.com/421p) 29 | 30 | * [github repository](https://github.com/421p/slack-to-telegram-bot) 31 | * [docker image](https://hub.docker.com/r/421p/slack-to-telegram-bot) 32 | 33 | ## Depencencies 34 | - [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) 35 | - [slackclient](https://github.com/slackapi/python-slackclient) 36 | 37 | Install the dependencies via pip: `pip install -r requirements.txt`. 38 | 39 | ## License 40 | Licensed under the [Unlicense](http://unlicense.org/). 41 | Do with it whatever you want. 42 | --------------------------------------------------------------------------------