Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
dotX12 committed Mar 9, 2022
2 parents 39a9037 + 99103c3 commit 6bc41df
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 11 deletions.
Empty file added examples/send_audio/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions examples/send_audio/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from aiohttp import web

from waio import Bot, Dispatcher
from waio.logs import loguru_filter
from waio.types import Message

loguru_filter.set_level('DEBUG')

bot = Bot(
apikey='API_KEY',
src_name='SRC_NAME',
phone_number=0000000000
)

dp = Dispatcher(bot=bot)


@dp.message_handler(commands=['start'])
async def start_command(message: Message):
await message.bot.send_audio(
receiver=message.sender_number,
url="https://www.buildquickbots.com/whatsapp/media/sample/audio/sample01.mp3"
)


async def handler_gupshup(request):
event = await request.json()
await dp.handle_event(event)
return web.Response(status=200)


if __name__ == "__main__":
webhook = web.Application()
webhook.add_routes([web.post('/api/v1/gupshup/hook', handler_gupshup)])
web.run_app(webhook, port=8008)
Empty file added examples/send_file/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions examples/send_file/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from aiohttp import web

from waio import Bot, Dispatcher
from waio.logs import loguru_filter
from waio.types import Message

loguru_filter.set_level('DEBUG')

bot = Bot(
apikey='API_KEY',
src_name='SRC_NAME',
phone_number=0000000000
)

dp = Dispatcher(bot=bot)


@dp.message_handler(commands=['start'])
async def start_command(message: Message):
await message.bot.send_file(
receiver=message.sender_number,
url='https://www.buildquickbots.com/whatsapp/media/sample/pdf/sample01.pdf',
filename='test_file.pdf',
caption='Test Caption'
)


async def handler_gupshup(request):
event = await request.json()
await dp.handle_event(event)
return web.Response(status=200)


if __name__ == "__main__":
webhook = web.Application()
webhook.add_routes([web.post('/api/v1/gupshup/hook', handler_gupshup)])
web.run_app(webhook, port=8008)
Empty file.
35 changes: 35 additions & 0 deletions examples/send_sticker/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from aiohttp import web

from waio import Bot, Dispatcher
from waio.logs import loguru_filter
from waio.types import Message

loguru_filter.set_level('DEBUG')

bot = Bot(
apikey='API_KEY',
src_name='SRC_NAME',
phone_number=0000000000
)

dp = Dispatcher(bot=bot)


@dp.message_handler(commands=['start'])
async def start_command(message: Message):
await message.bot.send_sticker(
receiver=message.sender_number,
url="http://www.buildquickbots.com/whatsapp/stickers/SampleSticker01.webp",
)


async def handler_gupshup(request):
event = await request.json()
await dp.handle_event(event)
return web.Response(status=200)


if __name__ == "__main__":
webhook = web.Application()
webhook.add_routes([web.post('/api/v1/gupshup/hook', handler_gupshup)])
web.run_app(webhook, port=8008)
Empty file added examples/send_video/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions examples/send_video/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from aiohttp import web

from waio import Bot, Dispatcher
from waio.logs import loguru_filter
from waio.types import Message

loguru_filter.set_level('DEBUG')

bot = Bot(
apikey='API_KEY',
src_name='SRC_NAME',
phone_number=0000000000
)

dp = Dispatcher(bot=bot)


@dp.message_handler(commands=['start'])
async def start_command(message: Message):
await message.bot.send_video(
receiver=message.sender_number,
url="https://www.buildquickbots.com/whatsapp/media/sample/video/sample01.mp4",
caption="Test video caption"
)


async def handler_gupshup(request):
event = await request.json()
await dp.handle_event(event)
return web.Response(status=200)


if __name__ == "__main__":
webhook = web.Application()
webhook.add_routes([web.post('/api/v1/gupshup/hook', handler_gupshup)])
web.run_app(webhook, port=8008)
51 changes: 45 additions & 6 deletions waio/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
from waio.keyboard.list import ListMessage
from waio.keyboard.reply import QuickReply
from waio.labeler import BotLabeler
from waio.logs.logger import logger
from waio.middleware import MiddlewareResponse
from waio.models.audio import AudioModel
from waio.models.enums import GupshupMethods
from waio.models.file import FileModel
from waio.models.image import ImageModel
from waio.models.sticker import StickerModel
from waio.models.text import MessageText
from waio.models.video import VideoModel
from waio.states.context import FSMContext
from waio.storage.redis import RedisStorage
from waio.types.message import Message
from waio.logs.logger import logger


class Bot(GupshupSettings, HTTPClient):
Expand Down Expand Up @@ -48,15 +52,50 @@ async def send_message(self, receiver: int, message: str):
return await self._base_request(receiver=receiver, data=msg)

async def send_image(
self,
receiver: int,
original_url: str,
preview_url: Optional[str] = None,
caption: Optional[str] = None,
self,
receiver: int,
original_url: str,
preview_url: Optional[str] = None,
caption: Optional[str] = None,
):
image = ImageModel(original_url=original_url, preview_url=preview_url, caption=caption)
return await self._base_request(receiver=receiver, data=image)

async def send_file(
self,
receiver: int,
url: str,
filename: str,
caption: Optional[str] = None,
):
file = FileModel(url=url, filename=filename, caption=caption)
return await self._base_request(receiver=receiver, data=file)

async def send_video(
self,
receiver: int,
url: str,
caption: Optional[str] = None,
):
video = VideoModel(url=url, caption=caption)
return await self._base_request(receiver=receiver, data=video)

async def send_audio(
self,
receiver: int,
url: str,
):
audio = AudioModel(url=url)
return await self._base_request(receiver=receiver, data=audio)

async def send_sticker(
self,
receiver: int,
url: str,
):
sticker = StickerModel(url=url)
return await self._base_request(receiver=receiver, data=sticker)

async def send_list(self, receiver: int, keyboard: ListMessage):
return await self._base_request(receiver=receiver, data=keyboard)

Expand Down
19 changes: 19 additions & 0 deletions waio/models/audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ujson


class AudioModel:
def __init__(
self,
url: str,
):
self.type = 'audio'
self.url = url

def dict(self):
return {
"type": self.type,
"url": self.url,
}

def json(self):
return ujson.dumps(self.dict(), indent=2)
27 changes: 27 additions & 0 deletions waio/models/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Optional

import ujson


class FileModel:
def __init__(
self,
url: str,
filename: str,
caption: Optional[str] = None
):
self.type = 'file'
self.url = url
self.filename = filename
self.caption = caption

def dict(self):
return {
"type": self.type,
"url": self.url,
"filename": self.filename,
"caption": self.caption,
}

def json(self):
return ujson.dumps(self.dict(), indent=2)
19 changes: 19 additions & 0 deletions waio/models/sticker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ujson


class StickerModel:
def __init__(
self,
url: str,
):
self.type = 'sticker'
self.url = url

def dict(self):
return {
"type": self.type,
"url": self.url,
}

def json(self):
return ujson.dumps(self.dict(), indent=2)
25 changes: 25 additions & 0 deletions waio/models/video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Optional

import ujson


class VideoModel:
def __init__(
self,
url: str,
caption: Optional[str] = None

):
self.type = 'video'
self.url = url
self.caption = caption

def dict(self):
return {
"type": self.type,
"url": self.url,
"caption": self.caption,
}

def json(self):
return ujson.dumps(self.dict(), indent=2)
42 changes: 37 additions & 5 deletions waio/protocols/bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Optional
from typing import Protocol

from waio.keyboard.list import ListMessage
from waio.keyboard.reply import QuickReply

Expand All @@ -16,10 +17,41 @@ async def send_reply(self, receiver: int, keyboard: QuickReply):
"""Send reply message"""

async def send_image(
self,
receiver: int,
original_url: str,
preview_url: Optional[str] = None,
caption: Optional[str] = None,
self,
receiver: int,
original_url: str,
preview_url: Optional[str] = None,
caption: Optional[str] = None,
):
"""Send image message"""

async def send_file(
self,
receiver: int,
url: str,
filename: str,
caption: Optional[str] = None,
):
"""Send file message"""

async def send_video(
self,
receiver: int,
url: str,
caption: Optional[str] = None,
):
"""Send video message"""

async def send_audio(
self,
receiver: int,
url: str,
):
"""Send audio message"""

async def send_sticker(
self,
receiver: int,
url: str,
):
"""Send sticker message"""

0 comments on commit 6bc41df

Please sign in to comment.