5

I am trying to modify code from this Webpage: The modified code is as below:

import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime


import requests  
from bottle import (  
    run, post, response, request as bottle_request
)

BOT_URL = 'https://api.telegram.org/bot------------------/' 


def get_chat_id(data):  
    """
    Method to extract chat id from telegram request.
    """
    chat_id = data['message']['chat']['id']

    return chat_id

def get_message(data):  
    """
    Method to extract message id from telegram request.
    """
    message_text = data['message']['text']

    return message_text

def send_message(prepared_data):  
    """
    Prepared data should be json which includes at least `chat_id` and `text`
    """ 
    message_url = BOT_URL + 'sendMessage'
    requests.post(message_url, json=prepared_data)  # don't forget to make import requests lib


def get_ticker(text):  # <-- **added this function and removed a function called `def change_text_message(text)`**;
    stock = f'text'
    start = datetime.date(2000,1,1)
    end = datetime.date.today()
    data = web.DataReader(stock, 'yahoo',start, end)
    plot = data.plot(y='Open')
    return plot


def prepare_data_for_answer(data):  
    answer = get_ticker(get_message(data))

    json_data = {
        "chat_id": get_chat_id(data),
        "text": answer,
    }

    return json_data

@post('/')
def main():  
    data = bottle_request.json

    answer_data = prepare_data_for_answer(data)
    send_message(answer_data)  # <--- function for sending answer

    return response  # status 200 OK by default

I have separated the code which I have modified with space above and below. At this point I am getting a text null, how can I fix this so when I enter a ticker, it returns the chart of the ticker? I am not sure the chart could be returned or if the only text could be sent back. The function which was added does work if run separately but just not here.

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60

2 Answers2

4

Maybe I don't understand something but the main reason is because your function(get_ticker) doesn't return anything.

Look at:

answer_data = prepare_data_for_answer(data). The result of prepare_data_for_answer:

{
    "chat_id": get_chat_id(data),
    "text": answer,
}

Ok. What is answer? Is result of get_ticker(see answer = get_ticker(get_message(data))).

Ok. But what is result of get_ticker? I do not see return statement... So the result is always None(json null). This is like:

def get_message():
    msg = 'hello'
message = get_message()  # None. Always None(or null in json)

Hope this helps.

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
  • I have added `return plot` which should solve this problem but i have run into another problem as it turns out JSON does not handle images – Slartibartfast Apr 15 '20 at 08:07
  • 1
    @newcoder first of all, _sorry but I won't fix all your problems_ step by step. of course `return plot` will not work because you trying to send `python object` via `json`. Anyway the main problem was with `prepare_data_for_answer`. At the moment your current problems/details is not related to original question. – Danila Ganchar Apr 15 '20 at 10:00
3

The first problem as answered by @DanilaG above, is that you didn't return anything from the prepare_data_for_answer. (I suggest if you fixed that, update your question so the code is relative to your new problem)

Your prepare_data_for_answer also expected get_ticker to return a value, but get_ticker doesn't return anything so it would be Null. In general, make sure everytime you set a variable to the return value of a function, the called function actually returns something.

I browsed over Telegram API and to send a photo you need a different API call /sendPhoto.

Check these SO answered questions as well:

Chen A.
  • 10,140
  • 3
  • 42
  • 61