~josealberto4444/apodnasabot

41b0f00ab129707e5d0355dfb6ef165ac6c329f1 — José Alberto Orejuela García 4 years ago e500f6f
Make the first version of the Telegram bot
6 files changed, 157 insertions(+), 7 deletions(-)

M Pipfile
M Pipfile.lock
M README.md
M api.py
M config.cfg.new
A telegram_bot.py
M Pipfile => Pipfile +1 -0
@@ 9,6 9,7 @@ verify_ssl = true
configparser = "*"
requests = "*"
youtube-dl = "*"
python-telegram-bot = "==12.0.0b1"

[requires]
python_version = "3.7"

M Pipfile.lock => Pipfile.lock +102 -1
@@ 1,7 1,7 @@
{
    "_meta": {
        "hash": {
            "sha256": "73772dd96e3e2d59447bf352706f2fba46223342938b2f8c3144d678929e6b09"
            "sha256": "1a45dd2b8ce488a5f2402eaff93515cae0e3335a02e9fc79c63f7944eb7fc3f3"
        },
        "pipfile-spec": 6,
        "requires": {


@@ 16,6 16,13 @@
        ]
    },
    "default": {
        "asn1crypto": {
            "hashes": [
                "sha256:2f1adbb7546ed199e3c90ef23ec95c5cf3585bac7d11fb7eb562a3fe89c64e87",
                "sha256:9d5c20441baf0cb60a4ac34cc447c6c189024b6b4c6cd7877034f4965c464e49"
            ],
            "version": "==0.24.0"
        },
        "certifi": {
            "hashes": [
                "sha256:046832c04d4e752f37383b628bc601a7ea7211496b4638f6514d0e5b9acc4939",


@@ 23,6 30,39 @@
            ],
            "version": "==2019.6.16"
        },
        "cffi": {
            "hashes": [
                "sha256:041c81822e9f84b1d9c401182e174996f0bae9991f33725d059b771744290774",
                "sha256:046ef9a22f5d3eed06334d01b1e836977eeef500d9b78e9ef693f9380ad0b83d",
                "sha256:066bc4c7895c91812eff46f4b1c285220947d4aa46fa0a2651ff85f2afae9c90",
                "sha256:066c7ff148ae33040c01058662d6752fd73fbc8e64787229ea8498c7d7f4041b",
                "sha256:2444d0c61f03dcd26dbf7600cf64354376ee579acad77aef459e34efcb438c63",
                "sha256:300832850b8f7967e278870c5d51e3819b9aad8f0a2c8dbe39ab11f119237f45",
                "sha256:34c77afe85b6b9e967bd8154e3855e847b70ca42043db6ad17f26899a3df1b25",
                "sha256:46de5fa00f7ac09f020729148ff632819649b3e05a007d286242c4882f7b1dc3",
                "sha256:4aa8ee7ba27c472d429b980c51e714a24f47ca296d53f4d7868075b175866f4b",
                "sha256:4d0004eb4351e35ed950c14c11e734182591465a33e960a4ab5e8d4f04d72647",
                "sha256:4e3d3f31a1e202b0f5a35ba3bc4eb41e2fc2b11c1eff38b362de710bcffb5016",
                "sha256:50bec6d35e6b1aaeb17f7c4e2b9374ebf95a8975d57863546fa83e8d31bdb8c4",
                "sha256:55cad9a6df1e2a1d62063f79d0881a414a906a6962bc160ac968cc03ed3efcfb",
                "sha256:5662ad4e4e84f1eaa8efce5da695c5d2e229c563f9d5ce5b0113f71321bcf753",
                "sha256:59b4dc008f98fc6ee2bb4fd7fc786a8d70000d058c2bbe2698275bc53a8d3fa7",
                "sha256:73e1ffefe05e4ccd7bcea61af76f36077b914f92b76f95ccf00b0c1b9186f3f9",
                "sha256:a1f0fd46eba2d71ce1589f7e50a9e2ffaeb739fb2c11e8192aa2b45d5f6cc41f",
                "sha256:a2e85dc204556657661051ff4bab75a84e968669765c8a2cd425918699c3d0e8",
                "sha256:a5457d47dfff24882a21492e5815f891c0ca35fefae8aa742c6c263dac16ef1f",
                "sha256:a8dccd61d52a8dae4a825cdbb7735da530179fea472903eb871a5513b5abbfdc",
                "sha256:ae61af521ed676cf16ae94f30fe202781a38d7178b6b4ab622e4eec8cefaff42",
                "sha256:b012a5edb48288f77a63dba0840c92d0504aa215612da4541b7b42d849bc83a3",
                "sha256:d2c5cfa536227f57f97c92ac30c8109688ace8fa4ac086d19d0af47d134e2909",
                "sha256:d42b5796e20aacc9d15e66befb7a345454eef794fdb0737d1af593447c6c8f45",
                "sha256:dee54f5d30d775f525894d67b1495625dd9322945e7fee00731952e0368ff42d",
                "sha256:e070535507bd6aa07124258171be2ee8dfc19119c28ca94c9dfb7efd23564512",
                "sha256:e1ff2748c84d97b065cc95429814cdba39bcbd77c9c85c89344b317dc0d9cbff",
                "sha256:ed851c75d1e0e043cbf5ca9a8e1b13c4c90f3fbd863dacb01c0808e2b5204201"
            ],
            "version": "==1.12.3"
        },
        "chardet": {
            "hashes": [
                "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",


@@ 38,6 78,33 @@
            "index": "pypi",
            "version": "==3.8.1"
        },
        "cryptography": {
            "hashes": [
                "sha256:24b61e5fcb506424d3ec4e18bca995833839bf13c59fc43e530e488f28d46b8c",
                "sha256:25dd1581a183e9e7a806fe0543f485103232f940fcfc301db65e630512cce643",
                "sha256:3452bba7c21c69f2df772762be0066c7ed5dc65df494a1d53a58b683a83e1216",
                "sha256:41a0be220dd1ed9e998f5891948306eb8c812b512dc398e5a01846d855050799",
                "sha256:5751d8a11b956fbfa314f6553d186b94aa70fdb03d8a4d4f1c82dcacf0cbe28a",
                "sha256:5f61c7d749048fa6e3322258b4263463bfccefecb0dd731b6561cb617a1d9bb9",
                "sha256:72e24c521fa2106f19623a3851e9f89ddfdeb9ac63871c7643790f872a305dfc",
                "sha256:7b97ae6ef5cba2e3bb14256625423413d5ce8d1abb91d4f29b6d1a081da765f8",
                "sha256:961e886d8a3590fd2c723cf07be14e2a91cf53c25f02435c04d39e90780e3b53",
                "sha256:96d8473848e984184b6728e2c9d391482008646276c3ff084a1bd89e15ff53a1",
                "sha256:ae536da50c7ad1e002c3eee101871d93abdc90d9c5f651818450a0d3af718609",
                "sha256:b0db0cecf396033abb4a93c95d1602f268b3a68bb0a9cc06a7cff587bb9a7292",
                "sha256:cfee9164954c186b191b91d4193989ca994703b2fff406f71cf454a2d3c7327e",
                "sha256:e6347742ac8f35ded4a46ff835c60e68c22a536a8ae5c4422966d06946b6d4c6",
                "sha256:f27d93f0139a3c056172ebb5d4f9056e770fdf0206c2f422ff2ebbad142e09ed",
                "sha256:f57b76e46a58b63d1c6375017f4564a28f19a5ca912691fd2e4261b3414b618d"
            ],
            "version": "==2.7"
        },
        "future": {
            "hashes": [
                "sha256:67045236dcfd6816dc439556d009594abf643e5eb48992e36beac09c2ca659b8"
            ],
            "version": "==0.17.1"
        },
        "idna": {
            "hashes": [
                "sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",


@@ 45,6 112,21 @@
            ],
            "version": "==2.8"
        },
        "pycparser": {
            "hashes": [
                "sha256:0fcb4563dc88dca5c1151ac995537ea27783fe4707eb59bdacb39308b7f754bd",
                "sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3"
            ],
            "version": "==2.19"
        },
        "python-telegram-bot": {
            "hashes": [
                "sha256:29f7397ef7c697bc8a3bef3c26895879258d7a8257daf535259a2dd12b8a0f67",
                "sha256:a5a520ac3fbfda0f35ecfe3c953a2e220a4ef8a03e6117b46bd24d75580bd277"
            ],
            "index": "pypi",
            "version": "==12.0.0b1"
        },
        "requests": {
            "hashes": [
                "sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",


@@ 53,6 135,25 @@
            "index": "pypi",
            "version": "==2.22.0"
        },
        "six": {
            "hashes": [
                "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c",
                "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73"
            ],
            "version": "==1.12.0"
        },
        "tornado": {
            "hashes": [
                "sha256:349884248c36801afa19e342a77cc4458caca694b0eda633f5878e458a44cb2c",
                "sha256:398e0d35e086ba38a0427c3b37f4337327231942e731edaa6e9fd1865bbd6f60",
                "sha256:4e73ef678b1a859f0cb29e1d895526a20ea64b5ffd510a2307b5998c7df24281",
                "sha256:559bce3d31484b665259f50cd94c5c28b961b09315ccd838f284687245f416e5",
                "sha256:abbe53a39734ef4aba061fca54e30c6b4639d3e1f59653f0da37a0003de148c7",
                "sha256:c845db36ba616912074c5b1ee897f8e0124df269468f25e4fe21fe72f6edd7a9",
                "sha256:c9399267c926a4e7c418baa5cbe91c7d1cf362d505a1ef898fde44a07c9dd8a5"
            ],
            "version": "==6.0.3"
        },
        "urllib3": {
            "hashes": [
                "sha256:b246607a25ac80bedac05c6f282e3cdaf3afb65420fd024ac94435cabe6e18d1",

M README.md => README.md +11 -3
@@ 53,12 53,20 @@ This will do the following steps:

Then, you should [get an API key from NASA][api-request] and copy it in the corresponding section of `config.cfg`.

Then, you can run a test version of the state of the bot with
# Running

You can run the part that gets the API info with

    $ pipenv run python api.py

    $ ./start.sh
	
You can change the date of the APOD it downloads directly in `api.py`.

## Telegram bot

First, you should copy your bot token to the `config.cfg` file. Next, you can run the Telegram bot with

    $ pipenv run python telegram_bot.py





M api.py => api.py +3 -3
@@ 19,10 19,10 @@ import re
import requests
import youtube_dl

def read_api_key():
def read_config(section, key):
    config = configparser.ConfigParser()
    config.read('config.cfg')
    return config['NASA_API']['api_key']
    return config[section][key]

def ask_api(api_key, date):
    baseurl = 'https://api.nasa.gov/planetary/apod'


@@ 80,7 80,7 @@ def save_description(date, description):
date = '2019-07-10'

if not os.path.exists('data/' + date + '.json'):
    api_key = read_api_key()
    api_key = read_config('NASA_API', 'api_key')
    api_response = ask_api(api_key, date)
    if 'code' in api_response:
        if api_response['code'] == 400:

M config.cfg.new => config.cfg.new +3 -0
@@ 1,2 1,5 @@
[NASA_API]
api_key = DEMO_KEY

[Telegram]
token = 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw

A telegram_bot.py => telegram_bot.py +37 -0
@@ 0,0 1,37 @@
# Copyright © 2019 José Alberto Orejuela García (josealberto4444)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import configparser
import logging
import telegram.ext

def read_config(section, key):
    config = configparser.ConfigParser()
    config.read('config.cfg')
    return config[section][key]

def start(update, context):
    context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")

token = read_config('Telegram', 'token')
updater = telegram.ext.Updater(token=token, use_context=True)
dispatcher = updater.dispatcher

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

start_handler = telegram.ext.CommandHandler('start', start)
dispatcher.add_handler(start_handler)

updater.start_polling()