'Run a function from an external py file
Context: Once again i resort to stackoverflow gurus for assistance. I have a telegram chat bot that does chat management and has some functions. One of those functions is to send a wordcloud png file with some words after the user inputs the /startme command. I have an external py file that plots the words from tweeter for sentiment analysis and saves them to a directory, then i have a telegram bot that sends the plotted words after you enter /startme command. But the problem is that i want to run the script and generate the file dynamically from the telegram bot through the /command function. So everytime someone triggers the /startme command, the wordcloud.py script will plot the current words for that specific time the command is triggered, so its constantly outputting updated data without me having to cron or run the wordcloud py file manually.
Structure:
pybot
|_ sentiment <- folder
| |_ __init__.py <- empty file to let python know that there are modules here
| |_ wordcloud.py <- py script for wordcloud plot
|plottedimages <- folder containing the wordcloud plots
|tgbot.py <- my main bot
So, i have my wordcloud.py file that has a mainFunc that generates the wordcloud plot inside wordcloud.py that is in the sentiment folder.
I have my mainbot in the pybot folder, and i am importing the mainFunc like this:
from sentiment import wordcloud
in the main code i have this function for the /command:
update.message.reply_text('Here is your wordcloud plot')
mainFunc()
time.sleep(3)
update.message.bot.send_photo(update.message.chat.id,open('plottedimages/'+str(a)+"wordcloud.png", 'rb'))```
If i run the bot without importing everything works,however that implies i have to manually generate the wordcloud everytime and i want this to be dynamically on command. Croning it isnt ideal albeit it works. Preferably i want to use the tg bot / command to trigger the wordcloud plot that i have in my other directory. Any help python wizards? Sorry for long text, again im very new to python :)
**Edit:**
I am using time.sleep to give the wordcloud plot time to render and save it before it sends it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
