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.
A chatbot is a specific type of bot. However, there are multiple other uses for bots. If you can visualize a way to automate a process in Telegram, then you can probably find or write a bot to do the job for you. Bots can even offer their users HTML5 games to play solo or compete against each other in groups and one-on-one chats.
With File to Bot, you can save files to the cloud, with unlimited storage in Telegram. It is multilingual, and the first thing the bot asks you is to select from a range of languages. It then gives you a welcome message saying, "Welcome to the Filetobot Bot (@filetobot). Save your files here.
BotFather - @BotFather BotFather describes itself as the one Bot to rule them all. You can use it to create new bot accounts and manage your existing bots. When you go to BotFather within your Telegram app, it links you to help files about Telegram bots as well as the Bot API manual.
If you can visualize a way to automate a process in Telegram, then you can probably find or write a bot to do the job for you. Bots can even offer their users HTML5 games to play solo or compete against each other in groups and one-on-one chats. Bots can keep track of high scores for every game played in every chat.
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.
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:
How to send photo on telegram bot
Sending message in telegram bot with images
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With