To staart, I am pretty new to coding and this is somewhat of a big project for me. I am trying to make a twitter bot that would run automatically, however, I keep getting "Unicode objects are not escapbable" as a result when running it on a Heroku sever.
The code works fine in Pycharm, but for some reason will not work when running on Heroku. Here is what the beginning looks like:
import tweepy
import statsapi
import time
import os
#enviroment variables
from dotenv import load_dotenv
load_dotenv()
#Keys
CONSUMER_KEY = os.getenv("CONSUMER_KEY")
CONSUMER_SECRET = os.getenv("CONSUMER_SECRET")
ACCESS_TOKEN = os.getenv('ACCESS_TOKEN')
ACCESS_TOKEN_SECRET = os.getenv('ACCESS_TOKEN_SECRET')
#
while True:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
Related
My bot.py 's code is :
import discord
from discord.ext import commands
import random
from discord.ext.commands import bot
import asyncio
import requests
import os
client = commands.Bot(command_prefix = "!")
.. Command's and stuff..
bot.run(str(os.environ.get('DISCORD_TOKEN')))
My requirements.txt consists of :
discord.py
requests
My Procfile consists of :
worker: python3 bot.py
I followed all steps to deploy through git and it successfully got deployed but still it's not online.
You bot doesn't go online because of your os.environ. It doesn't have any get method since it's a Mapping object. Here's how you use it:
bot.run(os.environ['DISCORD_TOKEN'])
PS: you have some unecessary imports, like from discord.ext.commands import bot. You should only import what you need (eg. from os import environ instead of import os).
I am trying to deploy my python telegram bot on heroku. Everything builds fine and heroku said its deployed successfully. However, when I tried the bot in telegram, it does not work. I have attached the deployment code below. Can someone help please? Thanks. My Procfile contains this: web: python3 encouragements.py
`import os
TOKEN = "Token"
PORT = int(os.environ.get('PORT', '5000'))
updater = Updater("Token")
updater.start_webhook(listen="0.0.0.0",
port=PORT,
url_path="Token")
updater.bot.setWebhook("https://xxx.herokuapp.com/" + "Token")
updater.idle()`
I found this article helpful when deploying a Telegram bot on Heroku: Creating Telegram Bot and Deploying it to Heroku (make sure the code is up to date by comparing it to the docs guide to Version 12.0)
Based on the article provided above, I've tried to reproduce your case with the following setup:
encouragements.py:
from telegram.ext import Updater, CommandHandler, CallbackContext
from telegram import Update
import os
TOKEN = ""
HEROKU_APP_NAME=""
# def run(updater):
# updater.start_polling()
def run(updater):
PORT = int(os.environ.get("PORT", "8443"))
updater.start_webhook(listen="0.0.0.0",
port=PORT,
url_path=TOKEN)
updater.bot.set_webhook("https://{}.herokuapp.com/{}".format(HEROKU_APP_NAME, TOKEN))
def start_handler(update: Update, context: CallbackContext):
update.message.reply_text("Hello from Python!\nPress /random to get random number")
if __name__ == '__main__':
updater = Updater(TOKEN, use_context=True)
updater.dispatcher.add_handler(CommandHandler("start", start_handler))
run(updater)
Pipfile:
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
python-telegram-bot = "*"
[requires]
python_version = "3.7"
Procfile:
web: python encouragements.py
And telegram bot is indeed responding to /start message. I followed this article when deploying to Heroku: https://devcenter.heroku.com/articles/getting-started-with-nodejs
I would also recommend to check what is happening on Heroku's side:
heroku logs -t --app <your-heroku-app-name>
Logs should tell you if your token is in fact correct, if your dependencies where properly loaded, and if your code does not produce any error during runtime.
Let me know if it worked :)
I need to periodically delete files of an uploads folder on heroku in python.
I've tried the heroku scheduler but I am constantly getting a fileNotFound error which I am unable to fix.
from flask_script import Manager
from app import app
manager = Manager(app)
#manager.command
def hello():
folder_path = '/uploads/'
for file_object in os.listdir(folder_path):
file_object_path = os.path.join(folder_path, file_object)
if os.path.isfile(file_object_path):
os.unlink(file_object_path)
else:
shutil.rmtree(file_object_path)
I am a beginner and I have a simple application I have developed locally which uses mongodb with mongoKit as follows:
app = Flask(__name__)
app.config.from_object(__name__)
customerDB = MongoKit(app)
customerDB.register([CustomerModel])
then in views I just use the CustomerDB
I have put everything on heroku cloud but my database connection doesn't work.
I got the link I need to connect by:
heroku config | grep MONGOLAB_URI
but I am not sure how to pull this. I looked at the following post, but I am more confused
How can I use the mongolab add-on to Heroku from python?
Any help would be appreciated.
Thanks!
According to the documentation, Flask-MongoKit supports a set of configuration settings.
MONGODB_DATABASE
MONGODB_HOST
MONGODB_PORT
MONGODB_USERNAME
MONGODB_PASSWORD
The MONGOLAB_URI environment setting needs to be parsed to get each of these. We can use this answer to the question you linked to as a starting point.
import os
from urlparse import urlsplit
from flask import Flask
from flask_mongokit import MongoKit
app = Flask(__name__)
# Get the URL from the Heroku setting.
url = os.environ.get('MONGOLAB_URI', 'mongodb://localhost:27017/some_db_name')
# Parse it.
parsed - urlsplit(url)
# The database name comes from the path, minus the leading /.
app.config['MONGODB_DATABASE'] = parsed.path[1:]
if '#' in parsed.netloc:
# If there are authentication details, split the network locality.
auth, server = parsed.netloc.split('#')
# The username and password are in the first part, separated by a :.
app.config['MONGODB_USERNAME'], app.config['MONGODB_PASSWORD'] = auth.split(':')
else:
# Otherwise the whole thing is the host and port.
server = parsed.netloc
# Split whatever version of netloc we have left to get the host and port.
app.config['MONGODB_HOST'], app.config['MONGODB_PORT'] = server.split(':')
customerDB = MongoKit(app)
My django 1.6 project is structured:
cg1
cg1
settings.py
cont_proc.py
inti, etc.
app
app
manage.py
templates
cont_proc.py reads:
from django.conf import settings
def misc(request):
return {'SITE_URL': settings.SITE_URL,'BALANCED_API_KEY':settings.BALANCED_API_KEY}`
in settings.py I have:
import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + ( os.path.join(BASE_DIR, 'cg1.cont_proc.misc'),)
BALANCED_API_KEY = os.environ.get('BALANCED_API_KEY')
SITE_URL = 'www.mysite.com' #but set up
python manage.py shell:
>>> from django.conf import settings
>>> settings.TEMPLATE_CONTEXT_PROCESSORS
['django_balanced.context_processors.balanced_library','django_balanced.context_processors.balanced_settings', 'django.contrib.auth.context_processors.auth']
>>>>import os
>>>>os.environ.get('BALANCED_API_KEY')
'correct key from a local .env file'
I've tried quite a few so question, especially: Where is template context processor in Django 1.5?
also: Python/Django is importing the wrong module (relative when it should be absolute)
but django doesn't seem to see my custom context processor, cont_proc, in the shell. And when I use render in views my templates do not receive the variables.
I had installed django-balanced. Apparently this was a mistake. I removed from installed apps and all was good.