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

Added support for statistics tracking using botan.io #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 22 additions & 17 deletions README.md
@@ -1,5 +1,6 @@
# telebot
Telegram Bot starter kit. Very easy to install with Google App Engine.
Supports automatic statistics tracking with [Botan.io](https://github.com/botanio/sdk).

Reddit post: http://www.reddit.com/r/Telegram/comments/3b1pwl/create_your_own_telegram_bot_stepbystep/

Expand Down Expand Up @@ -48,47 +49,51 @@ Instructions

13. @botfather replies with `Success! The new status is: DISABLED. /help`

14. Go to https://console.developers.google.com/project
14. If you want to analyze usage statistics of your bot, you should register new bot using [@BotanioBot](https://telegram.me/botaniobot?start=src%3Dtelebot).

15. Click `Create Project`
After you receive your botan token you should replace `BOTAN_TOKEN = None` to `BOTAN_TOKEN = 'YOUR_BOTAN_TOKEN'`.

16. Type the project name, whatever you want. For example: `octopus-gorilla-123`. Make sure the Project ID is also the same.
15. Go to https://console.developers.google.com/project

16. Click `Create Project`

17. Type the project name, whatever you want. For example: `octopus-gorilla-123`. Make sure the Project ID is also the same.

![App Engine registration](http://i.imgur.com/mxw8owO.png)

17. Clone this repository. If you don't understand what I am saying, click the `Download ZIP` button on the lower-right of this page, and extract the ZIP file.
18. Clone this repository. If you don't understand what I am saying, click the `Download ZIP` button on the lower-right of this page, and extract the ZIP file.

18. Open `app.yaml` file using a good text editor like Sublime Text. Change the `YOUR_APP_ID_HERE` to the Project ID you set on step 16, and save the file.
19. Open `app.yaml` file using a good text editor like Sublime Text. Change the `YOUR_APP_ID_HERE` to the Project ID you set on step 16, and save the file.

![app.yaml](http://i.imgur.com/m9yRwNw.png)

19. Open `main.py` file using a good text editor. Change the `YOUR_BOT_TOKEN_HERE` to the token you get from @botfather at step 6, and save the file.
20. Open `main.py` file using a good text editor. Change the `YOUR_BOT_TOKEN_HERE` to the token you get from @botfather at step 6, and save the file.

![main.py](http://i.imgur.com/oNFEdsp.png)

20. Download Google App Engine SDK for Python from https://cloud.google.com/appengine/downloads and install it.
21. Download Google App Engine SDK for Python from https://cloud.google.com/appengine/downloads and install it.

21. Run the GoogleAppEngineLauncher application.
22. Run the GoogleAppEngineLauncher application.

22. Click the `File` menu, choose `Add Existing Application...` and browse to the folder with the `app.yaml` and `main.py` file.
23. Click the `File` menu, choose `Add Existing Application...` and browse to the folder with the `app.yaml` and `main.py` file.

23. The project ID should appear as a row at the `Name` column.
24. The project ID should appear as a row at the `Name` column.

![app engine launcher](http://i.imgur.com/SXr2Tz2.png)

24. Click `Deploy`, enter your Google credentials, and your app should be installed to Google's servers.
25. Click `Deploy`, enter your Google credentials, and your app should be installed to Google's servers.

25. Open your browser and go to https://`project-id`.appspot.com/me (replace `project-id` with the Project ID you set on step 16).
26. Open your browser and go to https://`project-id`.appspot.com/me (replace `project-id` with the Project ID you set on step 16).

26. Wait until you see a long text with `"ok": true` and your bot's name. This could take a minute or so, please reload if it does not succeed.
27. Wait until you see a long text with `"ok": true` and your bot's name. This could take a minute or so, please reload if it does not succeed.

27. Now, go to https://`project-id`.appspot.com/set_webhook?url=https://`project-id`.appspot.com/webhook (replace both `project-id`s with the Project ID you set on step 16).
28. Now, go to https://`project-id`.appspot.com/set_webhook?url=https://`project-id`.appspot.com/webhook (replace both `project-id`s with the Project ID you set on step 16).

28. You should see `Webhook was set`.
29. You should see `Webhook was set`.

29. Open your Telegram client and send the message `/start` to your bot. (type @`your-bot-username` at the search field to initiate the conversation)
30. Open your Telegram client and send the message `/start` to your bot. (type @`your-bot-username` at the search field to initiate the conversation)

30. Try sending more messages and you should see replies from the bot. Mission completed!
31. Try sending more messages and you should see replies from the bot. Mission completed!

To customize the bot, edit the `main.py` starting from the `CUSTOMIZE FROM HERE` line. It is simple if-else statements. The starter kit calls simsimi.com to generate replies.

12 changes: 12 additions & 0 deletions main.py
Expand Up @@ -15,8 +15,10 @@
import webapp2

TOKEN = 'YOUR_BOT_TOKEN_HERE'
BOTAN_TOKEN = None # or 'YOUR_BOTAN_TOKEN'

BASE_URL = 'https://api.telegram.org/bot' + TOKEN + '/'
BOTAN_BASE_URL = 'https://api.botan.io/track'


# ================================
Expand Down Expand Up @@ -79,6 +81,16 @@ def post(self):
chat = message['chat']
chat_id = chat['id']

try:
if BOTAN_TOKEN is not None:
urllib2.urlopen('{botan_api}?token={token}&uid={uid}&name=MESSAGE_RECEIVED'.format(
botan_api=BOTAN_BASE_URL,
token=BOTAN_TOKEN,
uid=fr['id'] if fr is not None else chat_id,
), json.dumps(body)).read()
except Exception as e:
logging.error('Error tracking event: {}'.format(e))

if not text:
logging.info('no text')
return
Expand Down