Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WhatsApp interface #317

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dff/messengers/whatsapp_iface/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

try:
import whatsapp
except ImportError:
raise ImportError("whatsapp-python is not installed. Run `pip install dff[whatsapp]`")

from .interface import WhatsappInterface
76 changes: 76 additions & 0 deletions dff/messengers/whatsapp_iface/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import Optional, Any
from pydantic import SourcePath, HttpUrl

from whatsapp import WhatsApp, Message as WhatsAppMessage

from dff.script.core.message import Message, Audio, Video, Image, Document, Attachments
from dff.messengers.common import CallbackMessengerInterface
from dff.pipeline.types import PipelineRunnerFunction


def extract_message_from_whatsapp(message: WhatsAppMessage) -> Message: # pragma: no cover
inn_mess = Message()
inn_mess.text = message.content

if message.type == "audio":
files = [Audio(source=HttpUrl(message.audio["id"]))]
elif message.type == "video":
files = [Video(source=HttpUrl(message.video["id"]))]
elif message.type == "image":
files = [Image(source=HttpUrl(message.image["id"]))]
elif message.type == "document":
files = [Document(source=HttpUrl(message.document["id"]))]
else:
files = list()

inn_mess.attachments = Attachments(files=files)
return inn_mess


def cast_message_to_whatsapp_and_send(messenger: WhatsApp, to: Any, message: Message) -> None: # pragma: no cover
if message.attachments is not None:
attachment = next(message.attachments.files)
if isinstance(attachment.source, SourcePath):
media_id = messenger.upload_media(media=attachment.source)["id"]
else:
media_id = attachment.source
if isinstance(attachment, Audio):
messenger.send_audio(audio=media_id, recipient_id=to, link=False)
elif isinstance(attachment, Video):
messenger.send_video(video=media_id, recipient_id=to, caption=message.text, link=False)
pseusys marked this conversation as resolved.
Show resolved Hide resolved
elif isinstance(attachment, Image):
messenger.send_image(image=media_id, recipient_id=to, caption=message.text, link=False)
elif isinstance(attachment, Document):
messenger.send_document(document=media_id, recipient_id=to, caption=message.text, link=False)
else:
reply = messenger.create_message(content=message.text, to=to)
reply.send(True)


class WhatsappInterface(CallbackMessengerInterface): # pragma: no cover
def __init__(
self,
token: str,
phone_number_id: str,
host: str = "localhost",
port: int = 8443,
debug: Optional[bool] = None,
messenger: Optional[WhatsApp] = None,
**wsgi_options,
) -> None:
self.host = host
self.port = port
self.debug = debug if debug is not None else False
self.messenger = messenger if messenger is not None else WhatsApp(token, phone_number_id, self.debug)
self.wsgi_options = wsgi_options
self.messenger.on_message(self.on_message)

async def on_message(self, message: WhatsAppMessage):
message.mark_as_read()
resp = await self.on_request_async(extract_message_from_whatsapp(message), message.sender)
cast_message_to_whatsapp_and_send(self.whatsapp, message.sender, resp.last_response)


async def connect(self, callback: PipelineRunnerFunction):
await super().connect(callback)
self.messenger.run(host=self.host, port=self.port, debug=self.debug, **self.wsgi_options)
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@ omegaconf = { version = "*", optional = true }
cryptography = { version = "*", optional = true }
requests = { version = "*", optional = true }
pytelegrambotapi = { version = "*", optional = true }
whatsapp-python = { version = "*", optional = true }
opentelemetry-instrumentation = { version = "*", optional = true }
sqlalchemy = { version = "*", extras = ["asyncio"], optional = true }
opentelemetry-exporter-otlp = { version = ">=1.20.0", optional = true } # log body serialization is required
@@ -88,6 +89,7 @@ mysql = ["sqlalchemy", "asyncmy", "cryptography"]
postgresql = ["sqlalchemy", "asyncpg"]
ydb = ["ydb", "six"]
telegram = ["pytelegrambotapi"]
whatsapp = ["whatsapp-python"]
stats = ["opentelemetry-exporter-otlp", "opentelemetry-instrumentation", "requests", "tqdm", "omegaconf"]
benchmark = ["pympler", "humanize", "pandas", "altair", "tqdm"]

@@ -206,6 +208,7 @@ addopts = "--strict-markers"
markers = [
"docker: marks tests as requiring docker containers to work",
"telegram: marks tests as requiring telegram client API token to work",
"whatsapp: marks tests as requiring whatsapp client API token to work",
"slow: marks tests as slow (taking more than a minute to complete)",
"no_coverage: tests that either cannot run inside the `coverage` workflow or do not affect coverage stats",
"all: reserved by allow-skip",