Forum

New Telegram Bot Pl...
 
Notifications
Retirer tout

New Telegram Bot Plugin

21 Posts
2 Utilisateurs
0 Reactions
11 {numéro}K Vu
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 
[#7372]

Good morning, or good evening

For the last 3-4 weeks, I've been tinkering a bit with projeqtor for my brother. He wanted some new features so I had a go.One of them was a way to send notifications to users in a better way than mails. Indeed, he didn't really like the idea of receiving several emails for simple notifications such a ticket's state change. So I added a few lines to the "SqlElement.php" file so that, when sending mails, it first checks if the person has a custom field containing their Telegram id, and if so, sends the notification via Telegram.

However, I know that it won't be easy to maitain this feature trhough the future versions since I modified one of the main files, so I was wandering if it was possible to make it as a plugin. I've attached the modified file (for projeqtor v9.0.5). I've also used node-red to manage the bot in order to redirect new messages to a script (also attached as "telegram_bot.php").

Configuration:

  • First, add two custom fields to the Resource class: an "idTelegram" field (varchar) to store the telegram id (like "@ARandomId") and a chatIdTelegram field (varchar). Both need to be visible and you can put chatIdTelegram as readonly.
  • In SqlElement.php: You just need to replace "YOUR_URL" with the url of your server and "YOUR_BOT_TOKEN" with your Telegram bot token.
  • In telegram_bot.php: You need to replace "USER:PWD" with a valid projeqtor user and password, "YOUR_API_KEY" with the corresponding API key, "YOUR_URL" with the url of your server and "YOUR_BOT_TOKEN" with your Telegram bot token.

You can also change the message at the end of "telegram_bot.php" in the $msg variable (I wrote them in french sry).

Does this seem to be a good enough idea to make it a plugin ?

Anyway, I hope this will help others and maybe reach your interest


 
Posté : 6 Mar AM 10:033
(@babynus)
Posts: 14952
Membre Admin
 

Hi,

Your idea is interesting, replacing emails with chat interface.
I had a look at your code, and there are few things that will have to be improved :

  • parametering of all parameters you defined as Strings.
  • Trick : you can add in parameters.php
    $myParameter='YOUR_PARAMETER';
    then use is projeqtor code
    Parameter::getGlobalParameter('myParameter');
    We'll then just have to "move" the parameters from file to DB to intergate it, and possibly change it as user parameter...

  • translation of messages (we do not accept hard coded messages, with only exception in technical log writing in english)
  • why restrict to Telegram ?
  • I understand reasons that let you prefer Telegram to other chats, but I think this could be easily extended to many other chats, such as Whatsapp, Messenger, Discord, ..

Also it is difficult to imagine this as a plugin, but possibly it could be included in community version.
It's too late for V9.1 that is under testing before soon deployment, but we'll have a look at this for V9.2.
I recorded Ticket #5359.


 
Posté : 6 Mar PM 12:033
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 

Thank you very much !

Of course parametering would be a necessity if it were to become a full feature. I went for the easy way by hard-coding the messages cause it was only on a very small scale but I admit it's not optimal.
Concerning other chats, Messenger and Discord would be quite easy to implement but Whatsapp would be a bit tricky I believe since you can't easily make a Whatsapp bot.

Thank you very much for your reply and for considering my idea


 
Posté : 6 Mar PM 12:033
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 

Small correction, the line (in SqlElement.php)

$res = $resource->getSqlElementsFromCriteria ( array("idTelegram"=>str_replace("@tg@", "", $adr)), false );

should be

$res = $resource->getSqlElementsFromCriteria ( array("idTelegram"=>str_replace("@tg", "", $adr)), false );

(without the second "@")

and the line

array_push($tgUsers, $res[0]->chatIdTelegram);

should be

if (!in_array($res[0]->chatIdTelegram, $tgUsers)) {
    array_push($tgUsers, $res[0]->chatIdTelegram);
}

(sry, I've just found out about these problems)


 
Posté : 6 Mar PM 13:033
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 

Hello again,
I also added the possibility to create templates for the messages. It works exactly the same as email templates. I added a page and copied the email template class (modified so that it doesn't create html but a more suitable message for telegram). Though it's not perfect, it works pretty well !
Don't know if it would be interesting enough to implement it


 
Posté : 7 Mar PM 20:033
(@babynus)
Posts: 14952
Membre Admin
 

Yes, please post your files.
We'll have a look at oppotunity toi integrate it on V9.2


 
Posté : 8 Mar PM 15:033
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 

Sure,

Here is the modified SqlElement.php and the new TelegramTemplate.php.
Main modifications of SqlElement (from the last I posted) are on lines 5293, 5600 to 5638 and 5705.
Here are the SQL statements I used to modify the database:

INSERT INTO menu (id, name, idMenu, type, sortOrder, level, idle, menuClass, isAdminMenu, isLeavesSystemMenu) VALUES ( 200000000, 'menuTelegramTemplate', 88, 'object', 686, 'ReadWriteAutomation', 0, 'Automation', 0, 0);
INSERT INTO navigation (name, idParent, idMenu, idReport, sortOrder) VALUES ('menuTelegramTemplate', 129, 200000000, 0, 21);
CREATE TABLE telegramtemplate ( id INT(12) UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, template VARCHAR(500) DEFAULT NULL, idMailable INT(12) DEFAULT NULL COMMENT '12', idType INT(12) UNSIGNED DEFAULT NULL COMMENT '12', idle INT(1) UNSIGNED DEFAULT '0' COMMENT '1', PRIMARY KEY (id), KEY emailtemplateMailable (idMailable) );
ALTER TABLE mailtosend ADD idTelegramTemplate INT(12) UNSIGNED DEFAULT NULL;

And the not really needed icons are also attached.


 
Posté : 8 Mar PM 19:033
(@babynus)
Posts: 14952
Membre Admin
 

Thanks.
Added new files in the ticket for integration on V9.2.


 
Posté : 9 Mar AM 11:033
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 

Hello again, quick update
I've nearly finished implementing another feature to the bot. You are now able to create tickets via Telegram. You can define some fields and specify a "Ticket Model" in which you can set default values for frequent similar tickets. I'll post the files when it's completly done


 
Posté : 17 Mar PM 19:033
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Début du sujet
 

Update:
I added the possibility to create tickets directly from Telegram and to view information about tickets and activities (can easily be extended to other classes)

  • available commands:
    • /ticket (create a ticket)
    • /afficher (display element info)
    • /stop (stop current command)
    • /report (report a bug)
    • /about (sends info about the bot)
  • /ticket:
  • [list=1]

  • Asks the name of the ticket
  • Display fields that can be set
  • You can choose a template (more about below) with predefined field values
  • When you click on "Créer" (create), it prints a summary of the ticket and let you modify or save it
  • Ticket templates:
    • New menu in Automation
    • You can create ticket templates with predefined field values
    • When creating a ticket with the bot, you can choose a template and it will automatically set the values defined in the template
    • Currently available fields for ticket templates:
      • ticket type, project, activity, responsible, urgency, criticality, context (1 and 3, bc we only needed these two)
  • /afficher:
  • [list=1]

  • Asks you what type of element you want to view (currently available: Ticket, Activity)
  • Asks you to select a project, then a potential sub-project, and another potential sub-project (you can of course select just a general project and it will include everything below), only projects with tickets/activities are selectable, I will include (sometime in the future) the possibility to choose between selecting just a project or selecting it plus all its sub-projects
  • When a project is selected, a list of all the elements of the selected class of that project are displayed, ordered by status and priority (an emoji of the status + an emoji of the priority, if it exists, is added after the name, see below)
  • When an element is selected, a summary is printed (display fields are defined in the bot script in $elementInfo), similar to the one for when a ticket is created. From there, you can either return to project selection or:
  • [list=1]

  • Assign it to yourself (if status = created ("enregistré" in french))
  • Start work (if status = assigned, it is set to in progress)
  • Stop work if it is started
  • Status and priority emojis:
    • I just added a field to Statuses and Priorities to set an emoji representing the said stat./prio. (not optimal since I can only use UTF-16 emojis due to some limitations of the database encoding I believe)
    • These emojis are used for notifications and in the list of displayable elements
  • /report:
    • Sends a message to the hardcoded chatId (mine) with details about current status of the bot for the sender user. Not much but it can help debug some problems

    That's it ! (I think). I still have some ideas in mind and so does my client (my brother 🙂 ). Though there's still a lot of place for improvement (hardcoded values, repeated blocks, unclear statements, no translations), I'm pretty proud of that bot. I've linked the files for the bot and for the TicketTemplate menu.
    To make it work, I also had to insert some lines in the database and create some tables:

    • INSERT INTO menu (id, name, idMenu, type, sortOrder, level, idle, menuClass, isAdminMenu, isLeavesSystemMenu) VALUES ( 200000001, 'menuTicketTemplate', 88, 'object', 694, 'Project', 0, 'Automation', 0, 0);
    • INSERT INTO navigation (name, idParent, idMenu, idReport, sortOrder) VALUES ('menuTicketTemplate', 129, 200000001, 0, 51);
    • CREATE TABLE tickettemplate ( id INT(12) UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, idTicketType INT(12) UNSIGNED DEFAULT NULL COMMENT '12', idProject INT(12) UNSIGNED DEFAULT NULL COMMENT '12', idle INT(1) UNSIGNED DEFAULT '0' COMMENT '1', idContext1 INT(12) UNSIGNED DEFAULT NULL, idContext3 INT(12) UNSIGNED DEFAULT NULL, idActivity INT(12) UNSIGNED DEFAULT NULL COMMENT '12', idUrgency INT(12) UNSIGNED DEFAULT NULL COMMENT '12', PRIMARY KEY (id), KEY ticketTicketType (idTicketType), KEY projectProject (idProject), KEY ticketActivity (idActivity), ticketUrgency (idUrgency) );

    And here is my nodered flow:


     
    Posté : 2 Avr PM 23:044
    (@babynus)
    Posts: 14952
    Membre Admin
     

    Hi,

    Thanks for your contribution.
    I added your post as note on ticket to include it on comlmunity version.


     
    Posté : 6 Avr PM 13:044
    Louis Heredero
    (@baryhobal)
    Posts: 49
    Active Member
    Début du sujet
     

    Would it be easier if I created a github repo, so you don't have to do this every time I've made modifications ?


     
    Posté : 6 Avr PM 17:044
    Louis Heredero
    (@baryhobal)
    Posts: 49
    Active Member
    Début du sujet
     

    Oh and I just found a bug, the line after "//Come back to class selection" should be

    $state = 0;

    instead of

    $state = 100;

    (Hence the usefulness of a repo)


     
    Posté : 7 Avr AM 02:044
    (@babynus)
    Posts: 14952
    Membre Admin
     

    Yes, it can be a good idea.
    But please, keep informing use when you publixh changes in the repo ! 😉


     
    Posté : 9 Avr PM 17:044
    Louis Heredero
    (@baryhobal)
    Posts: 49
    Active Member
    Début du sujet
     

    Of course no problem, I'll make it and post the link when it's done (probably next week)


     
    Posté : 9 Avr PM 17:044
    Page 1 / 2
    Share:
    Retour en haut