skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search