I’ve created a web service solution so PI Notifications can send notifications through Telegram using Telegram Bot API and ASP.NET. You’ll need to deploy this solution to IIS, create a Telegram Bot and configure PI Notifications to send events through this Web Service.
Before getting to deploy this solution, a few considerations:
- Marina Siqueira (firstname.lastname@example.org) had the idea of sending PI Notifications to a message app and I helped her on choosing Telegram and then by coding the application.
- Required software: MS Web Deploy v3.5, PI Notifications 2012 (and the other necessary OSIsoft PI products, such as PI interfaces, PI Data Archive, PI AF), IIS – with Windows Authentication & .NET Extensibility 3.5 & ASP.NET 3.5
- To learn more about the Telegram Bot API, navigate to: https://core.telegram.org/bots/api
- The web service uses Windows Authentication to allow only users in the group Administrators (in the machine where the service is running) to access it. You can add more users/groups by changing the Web.config file or going to .NET Authorization rules in IIS. The PI Notifications Scheduler service needs to run under a user inside the Administrators group or an allowed group.
- The web service configuration creates a XML file that contains: your bot’s AuthToken, every contact and uses your Notification name as a file name. The XML file is in the folder called “App_Data”, I recommend that you set the permissions to this folder as to allow only the necessary users/groups to view or edit the folder. Remember that the user running the IIS application pool needs to have read and write access to the folder.
After downloading and unzipping the NotificationForTelegram.zip:
1. Deploying the package to IIS
1) Install IIS and add the Windows Authentication, ASP.NET 3.5 and .NET Extensibility 3.5 Feature to it.
2) Use the Microsoft Web Platform Installer to install Web Deploy 3.5 (https://www.microsoft.com/web/downloads/platform.aspx?templang=pt-br).
3) Copy the contents of the Web Deploy Package folder to your server with IIS and Web Deploy installed.
4) Open a Command Prompt as Administrator, go to the directory where the Web Deploy Package is and run the NotificationsForTelegram_WebDeployPackage.deploy.cmd file with /Y argument to install OR /T to check changes that will be made without installing.
By default, the website will be deployed to “Default Web Site/NotificationsForTelegram”, you can change that by changing the NotificationsForTelegram_WebDeployPackage.SetParameters.xml file before deployment.
I recommend that you refer to this link for more information on deploying Web Deploy Packages to your IIS, especially if you need to do it remotely: https://msdn.microsoft.com/en-us/library/ff356104(v=vs.110).aspx
5) You need to change the application pool running the Notifications for Telegram service to .NET 2.0: https://technet.microsoft.com/en-us/library/cc731755(v=ws.10).aspx .
6) The application pool that runs the web service needs to run under a user in the Administrators group: https://technet.microsoft.com/en-us/library/cc771170(v=ws.10).aspx .
In my case, the application pool running the NotificationsForTelegram is the DefaultAppPool.
7) Still on IIS, open NotificationsForTelegram, click on Authentication under IIS, disable Anonymous Authentication and enable Windows Authentication. Select Windows Authentication after enabling and click on Advanced Settings and set “Extended Protection” to Accept.
8) On IIS, under Default Web Site, right click “NotificationsForTelegram” and click on “Explore”. On the Windows Explorer window that opens, create a new folder called “App_Data”, don’t forget to set permissions to that folder as discussed above – remove all users that don’t need access to it.
9) Restart IIS by opening a command prompt as administrator and typing “iisreset”.
10) On IIS, click on NotificationsForTelegram and then click on “Browse”, your browser will open with the configuration page of NotificationsForTelegram. Copy and save the link to the service, as you’ll need it later. Don’t configure it just yet.
2. Creating a Telegram Bot
1) On telegram, start a conversation with @BotFather, by sending “/start” to it
2) Use the “/newbot” command to create a new bot
3) Type in the name of the bot you want to create
4) Type in the bot’s username. It needs to end with the word “bot”.
5) The BotFather will answer with a token to access the HTTP API. Save that token in a secure location.
6) Start a conversation with your new Bot. Ask that everyone who will be a subscriber of the notifications start a conversation with it.
I recommend that, instead of sending notifications to different users, you create a group with everyone that needs to receive the notification and add the bot into it. Then, when configuring the service, you can send notifications to that group. This helps managing who gets notifications or not, as you can easily manage the group inside Telegram.
3. Creating a Notification
1) Open PI System Explorer (32 bit)
2) On the tab “Contacts”, create a new item under “Delivery Endpoints”.
3) Enter a name to the Delivery Endpoint (for example, NotificationsForTelegram)
4) Under Delivery Channel, select “Web Service”.
5) Type in the address of the Web Service. It’s something like http://*YourHostname*/NotificationsForTelegram/Service.asmx
You can find out the link to your configuration website by opening IIS, navigating to NotificationsForTelegram and then clicking on “Browse” on the right pane. After it opens in your browser, copy the link and add “/Service.asmx” to the end.
And then select “Get Web Services”. Don’t test it just yet, as you haven’t configured the service yet.
6) Under Web Service, select “Service” and under “Default Web Method” select “sendNotification”
7) Check in the new Delivery Endpoint
Here, you can create a Notification or Notification Template or edit the ones you already have. I’m creating a test one below:
8) On the tab “Notifications”, create a new notification, set a name to it (save the name, as you’ll use it later) and set the trigger as you need.
9) Under the tab “Subscriptions”, select “Add a new Subscriber” and select the Delivery Endpoint you just created.
10) Under “Parameters” fill the value field as below:
11) Check-in the changes.
4. Configuring Web Service:
1) Access the NotificationsForTelegram configuration website on the computer that runs the WebService via: http://localhost/NotificationsForTelegram/
If this isn’t the correct link, you can find out the link to your configuration website by opening IIS, navigating to NotificationsForTelegram and then clicking on “Browse” on the right pane.
2) Type in the AuthToken with the token given by the BotFather and click on “Retrieve Contacts”. If the AuthToken is correct and the internet can be reached, the List Box will be populated with Chat IDs and Names of every contact (or group) that started a conversation with the Bot in the last few hours.
3) Select the contacts and/or groups that you want the Notifications to be sent to. Hold the Control key to select multiple contacts.
4) If you want, you can send a Test Message to the selected users to check if the Bot is working correctly.
5) Type in the Notification name that will send messages to the selected contacts.
6) Select “Generate XML” to create a XML with the Bot’s AuthToken, Chat IDs and PI Notification IP Address.
Run the notification that you created before.
You’ll need to generate different XMLs for different notifications. This way, you can send different notifications to different users/group