-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6afebde
commit 85b43e3
Showing
3 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |