Skip to content

Commit

Permalink
added more telegram responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmarex committed Jan 10, 2020
1 parent 167fb17 commit cb63add
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
14 changes: 13 additions & 1 deletion pydialogflow_fulfillment/dialogflow_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ def get_single_ouputcontext(self,context_name):
return dict()

def get_user_token(self):
return self.request_data['originalDetectIntentRequest']['payload']['user']['idToken']
return self.request_data['originalDetectIntentRequest']['payload']['user']['idToken']

def get_chat_source(self):
"""
Get the platform from which the user is interacting from
"""
return self.request_data['originalDetectIntentRequest']['source']

def get_chat_platform_payload(self):
"""
Get the data been sent from the chat platform
"""
return self.request_data['originalDetectIntentRequest']['payload']
9 changes: 8 additions & 1 deletion pydialogflow_fulfillment/dialogflow_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self, fulfillment_message="", webhook_source="webhook"):
self.response_payload = OrderedDict()
self.rich_response = OrderedDict()
self.google_payload = OrderedDict()
self.telegram_payload = OrderedDict()
self.fulfillment_messages = []
self.expect_user_response = True
self.output_contexts = []
Expand All @@ -18,13 +19,15 @@ def __init__(self, fulfillment_message="", webhook_source="webhook"):

def __str__(self):
self.response_payload["google"] = self.google_payload
self.response_payload["telegram"] = self.telegram_payload
self.dialogflow_response["fulfillmentText"] = self.fulfillment_messages
self.dialogflow_response["outputContexts"] = self.output_contexts
self.dialogflow_response["payload"] = self.response_payload
return json.dumps(self.dialogflow_response)

def get_final_response(self):
self.response_payload["google"] = self.google_payload
self.response_payload["telegram"] = self.telegram_payload
self.dialogflow_response["fulfillmentText"] = self.fulfillment_messages
self.dialogflow_response["outputContexts"] = self.output_contexts
self.dialogflow_response["payload"] = self.response_payload
Expand Down Expand Up @@ -82,7 +85,11 @@ def add(self, dialog_response):
self.google_payload['userStorage'] = str(dialog_response.response)
# telegram responses
elif isinstance(dialog_response, TelegramSimpleResponse):
self.fulfillment_messages.append(dialog_response.response)
self.fulfillment_messages.append(dialog_response.response)
elif isinstance(dialog_response, TelegramKeyboardButtonResponse):
self.telegram_payload = dialog_response.response
elif isinstance(dialog_response, TelegramMessageResponse):
self.telegram_payload = dialog_response.response

self.google_payload["richResponse"] = self.rich_response
self.google_payload["expectUserResponse"] = self.expect_user_response
57 changes: 55 additions & 2 deletions pydialogflow_fulfillment/telegram_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,65 @@
import json

class TelegramSimpleResponse:

"""
Simple telegram response
message: str
"""
def __init__(self, message):
self.response = OrderedDict()
simple_reponse = OrderedDict()
responses_list = []
responses_list.append(message)
simple_reponse["text"] = responses_list
self.response["text"] = simple_reponse
self.response["platform"] = "TELEGRAM"
self.response["platform"] = "TELEGRAM"

class TelegramMessageResponse:
"""
message: str
- Text of the message to be sent. It may contain html or markdown.
Check https://core.telegram.org/bots/api/#markdown-style for supported markdown style and
https://core.telegram.org/bots/api/#html-style for supported html style
(Optional) parse_mode: str
- "html" or "markdown"
(Optional) reply_to_message_id: int
- If the message is a reply, ID of the original message
(Optional) disable_notification: Boolean
- Default: False
(Optional) disable_web_page_preview: Boolean
- Default: False
"""
def __init__(self, message, parse_mode="", reply_message_id=None, disable_notification=False, disable_web_page_preview=False):
self.response = OrderedDict()
self.response["text"] = message
self.response["parse_mode"] = parse_mode
self.response["disable_notification"] = disable_notification
self.response["disable_web_page_preview"] = disable_web_page_preview
if reply_message_id is not None:
self.response["reply_to_message_id"] = reply_message_id

class TelegramKeyboardButtonResponse:
"""
message: str
- Text of the message to be sent.
inline_keyboard : list
- Array of button rows, each represented by an Array of InlineKeyboardButton objects
Example:
inline_keyboard = [
{
"text": "A",
"callback_data": "A1"
},
{
"text": "B",
"callback_data": "C1"
}
]
"""

def __init__(self, message, inline_keyboard = []):
self.response = OrderedDict()
self.response["text"] = message
reply_markup = dict()
reply_markup["inline_keyboard"] = inline_keyboard
self.response["reply_markup"] = reply_markup

0 comments on commit cb63add

Please sign in to comment.