Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
Merge 113b0cc into d6589fc
Browse files Browse the repository at this point in the history
  • Loading branch information
wochinge committed Oct 17, 2018
2 parents d6589fc + 113b0cc commit 88aaed2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ Fixed
- argument ``--connector`` on run script accepts custom channel module names
- properly handle non ascii categorical slot values, e.g. ``大于100亿元``
- fixed HTTP server attempting to authenticate based on incorrect path to the correct JWT data field
- all sender ids from channels are now handled as `str`.
Sender ids from old messages with an `int` id are converted to `str`.


[0.11.12] - 2018-10-11
^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion rasa_core/channels/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self,
self.output_channel = CollectingOutputChannel()

if sender_id is not None:
self.sender_id = sender_id
self.sender_id = str(sender_id)
else:
self.sender_id = self.DEFAULT_SENDER_ID

Expand Down
10 changes: 10 additions & 0 deletions rasa_core/tracker_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ def save(self, tracker, timeout=None):

def retrieve(self, sender_id):
stored = self.conversations.find_one({"sender_id": sender_id})

# look for conversations which have used an `int` sender_id in the past
# and update them.
if stored is None and sender_id.isdigit():
from pymongo import ReturnDocument
stored = self.conversations.find_one_and_update(
{"sender_id": int(sender_id)},
{"$set": {"sender_id": str(sender_id)}},
return_document=ReturnDocument.AFTER)

if stored is not None:
if self.domain:
return DialogueStateTracker.from_dict(sender_id,
Expand Down
61 changes: 59 additions & 2 deletions tests/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from __future__ import print_function
from __future__ import unicode_literals

import requests

import json

from httpretty import httpretty
Expand Down Expand Up @@ -256,6 +254,65 @@ def test_telegram_channel():
httpretty.disable()


def test_handling_of_telegram_user_id():
# telegram channel will try to set a webhook, so we need to mock the api

httpretty.register_uri(
httpretty.POST,
'https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/setWebhook',
body='{"ok": true, "result": {}}')

# telegram will try to verify the user, so we need to mock the api
httpretty.register_uri(
httpretty.GET,
'https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/getMe',
body='{"result": {"id": 0, "first_name": "Test", "is_bot": true, '
'"username": "YOUR_TELEGRAM_BOT"}}')

# The channel will try to send a message back to telegram, so mock it.
httpretty.register_uri(
httpretty.POST,
'https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/sendMessage',
body='{"ok": true, "result": {}}')

httpretty.enable()

from rasa_core.channels.telegram import TelegramInput
from rasa_core.agent import Agent
from rasa_core.interpreter import RegexInterpreter

# load your trained agent
agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter())

input_channel = TelegramInput(
# you get this when setting up a bot
access_token="123:YOUR_ACCESS_TOKEN",
# this is your bots username
verify="YOUR_TELEGRAM_BOT",
# the url your bot should listen for messages
webhook_url="YOUR_WEBHOOK_URL"
)

from flask import Flask
import rasa_core
app = Flask(__name__)
rasa_core.channels.channel.register([input_channel],
app,
agent.handle_message,
route="/webhooks/")

data = {"message": {"chat": {"id": 1234, "type": "private"},
"text": "Hello", "message_id": 0, "date": 0},
"update_id": 0}
test_client = app.test_client()
test_client.post("http://localhost:5004/webhooks/telegram/webhook",
data=json.dumps(data),
content_type='application/json')

assert agent.tracker_store.retrieve("1234") is not None
httpretty.disable()


# USED FOR DOCS - don't rename without changing in the docs
def test_twilio_channel():
from rasa_core.channels.twilio import TwilioInput
Expand Down

0 comments on commit 88aaed2

Please sign in to comment.