Skip to content

Commit

Permalink
create bot simple code + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BaselAmin712 committed Jul 22, 2024
1 parent 6afebde commit 85b43e3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
8 changes: 6 additions & 2 deletions CONTRIBUTION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ Thank you for contributing to our project! To maintain a high standard of code q
## Before Working on an Issue

1. **Open a New Branch for the Issue**
- Always create a new branch while you are on the `main` branch.
- Always create a new branch while you are on the `dev` branch.
- Make sure the branch name includes the issue topic and number.
- **Do NOT** put the issue number at the beginning of the branch name.
- Example: `feature/issue-123-improve-login`
NOT:
NOT:
- `#34-new-login-button`

## Before Opening a Pull Request (PR) - YOU MUST:

1. **Review the Original Issue**

- Ensure you have completed all tasks and met all requirements specified in the issue.

2. **Clean Your Code**

- Remove unnecessary comments and spaces.
- Ensure logical positioning and proper naming conventions are followed.

3. **Add Tests and Error Handling**

- Make sure you have added testing for the main features and proper error handling.

4. **Verify Code Stability**

- Stop the running code and restart to ensure there are no errors and everything runs smoothly.

5. **Sync with Dev Branch**
Expand Down
47 changes: 47 additions & 0 deletions genie_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
import requests
import os
from dotenv import load_dotenv

load_dotenv()


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text('Hello! I am your bot. How can I help you?')

async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
help_text = (
"Available commands:\n"
"/start - Start the bot\n"
"/help - Show this help message\n"
"/ip - get public ip"
)
await update.message.reply_text(help_text)

async def get_public_ip(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
try:
response = requests.get('https://api.ipify.org?format=json')
public_ip = response.json()['ip']
await update.message.reply_text(f'The public IP address of the bot is: {public_ip}')
return True
except requests.RequestException as e:
await update.message.reply_text(f'Failed to get public IP address: {e}')
return False

def main():
# Create the Application and pass it your bot's token.
application = ApplicationBuilder().token(os.getenv('BOT_TOKEN')).build()

# Register the /start command handler
application.add_handler(CommandHandler("start", start))
# Register the /help command handler
application.add_handler(CommandHandler("help", help_command))
# Register the /ip command handler
application.add_handler(CommandHandler("ip", get_public_ip))

# Start the Bot
application.run_polling()

if __name__ == '__main__':
main()
53 changes: 53 additions & 0 deletions test_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
from unittest.mock import AsyncMock
from telegram import Update, Message
from telegram.ext import ContextTypes
from genie_bot import start, help_command, get_public_ip

@pytest.mark.asyncio
async def test_start():
update = AsyncMock(Update)
context = AsyncMock(ContextTypes.DEFAULT_TYPE)
message = AsyncMock(Message)
update.message = message

await start(update, context)

update.message.reply_text.assert_called_once_with('Hello! I am your bot. How can I help you?')

@pytest.mark.asyncio
async def test_help_command():
update = AsyncMock(Update)
context = AsyncMock(ContextTypes.DEFAULT_TYPE)
message = AsyncMock(Message)
update.message = message

await help_command(update, context)

help_text = (
"Available commands:\n"
"/start - Start the bot\n"
"/help - Show this help message"
)
update.message.reply_text.assert_called_once_with(help_text)

@pytest.mark.asyncio
async def test_get_public_ip():
update = AsyncMock(Update)
context = AsyncMock(ContextTypes.DEFAULT_TYPE)
message = AsyncMock(Message)
update.message = message

with patch('genie_bot.bot.requests.get') as mock_get:
mock_response = AsyncMock()
mock_response.json.return_value = {'ip': '123.123.123.123'}
mock_get.return_value = mock_response

await get_public_ip(update, context)

called_args = update.message.reply_text.call_args[0][0]
ip_address = called_args.split()[-1]

# Regular expression to validate an IPv4 address
ip_pattern = re.compile(r'^(\d{1,3}\.){3}\d{1,3}$')
assert ip_pattern.match(ip_address), f"IP address format is incorrect: {ip_address}"

0 comments on commit 85b43e3

Please sign in to comment.