Reputation: 45
I have the follow structure.
members
├── management
│ │── __init__.py
│ │── commands
│ │── active.py
│── whatsapp.py
│── config.ini
I am trying to run a whatsapp api and I have all my authentication data stored in a config.ini file. However, when I run python manage.py active I get the following error message.
C:\Users\timmeh\source\Python Projects\Django Projects\env\myproject\topxgym>python manage.py active
Traceback (most recent call last):
File "C:\Users\timmeh\source\Python Projects\Django Projects\env\myproject\topxgym\manage.py", line 22, in <module>
main()
File "C:\Users\timmeh\source\Python Projects\Django Projects\env\myproject\topxgym\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\timmeh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\timmeh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\timmeh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\timmeh\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 448, in execute
output = self.handle(*args, **options)
File "C:\Users\timmeh\source\Python Projects\Django Projects\env\myproject\topxgym\members\management\commands\active.py", line 20, in handle
msg = WhatsApp(name, phone, date)
File "C:\Users\timmeh\source\Python Projects\Django Projects\env\myproject\topxgym\members\management\commands\whatsapp.py", line 14, in __init__
self.authorization = self.config['Auth']['Authorization']
File "C:\Users\timmeh\AppData\Local\Programs\Python\Python310\lib\configparser.py", line 964, in __getitem__
raise KeyError(key)
KeyError: 'Auth'
The code works fine if I move it to a separate folder and run the whatsapp.py file directly.
active.py file
from turtle import update
from django.core.management.base import BaseCommand, CommandError
from members.models import ActiveMember
from datetime import datetime, timedelta
from .whatsapp import WhatsApp
class Command(BaseCommand):
help = 'Deactivate expired memberships!'
def handle(self, *args, **options):
ActiveMember.objects.filter(end_date__lt=datetime.now().date()).update(status='2')
expired_memberships = ActiveMember.objects.filter(end_date=datetime.now().date())
for expired in expired_memberships:
name = expired.member.full_name
phone = expired.member.phone
date = expired.end_date
msg = WhatsApp(name, phone, date)
msg.send_message()
whatsapp.py file
import requests
from configparser import ConfigParser
from datetime import datetime
class WhatsApp():
def __init__(self, name, phone, date):
self.required_len = len(name) + 2
self.config = ConfigParser()
self.config.read('config.ini')
self.name = name.center(self.required_len)
self.phone = add_suffix(self, phone)
self.date = date
self.authorization = self.config['Auth']['Authorization']
self.token = self.config['Auth']['Token']
self.phone_number_id = self.config['Auth']['phone_number_id']
self.whatsapp_business_account_id = self.config['Auth']['whatsapp_business_account_id']
def send_message(self):
headers = {
self.authorization: self.token,
}
json_data = {
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": self.phone,
"type": "template",
"template": {
"name": "customer_expire",
"language": {
"code": "en-us",
},
"components": [
{
"type": "body",
"parameters": [
{
"type": "text",
"text": self.name,
},
{
"type": "date_time",
"date_time": {
"fallback_value": str(self.date)
}
}
]
}
]
}
}
response = requests.post(f'https://graph.facebook.com/v14.0/{self.phone_number_id}/messages', headers=headers, json=json_data)
print('response = ', response)
print('response.status_code = ', response.status_code)
print('response.text = ', response.text)
def add_suffix(self, number):
number = number.lstrip('0')
if number[0] == '2':
return '64' + number
elif number[0] == '6' and number[1] == '4':
return number
else: return None
Upvotes: 0
Views: 136
Reputation: 45
For anyone that is having similar problem with Management Command. I placed my config.ini in main dir where my manage.py file is and that solved the problem.
Upvotes: 0